clang  19.0.0git
RewriteTest.cpp
Go to the documentation of this file.
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"
16 #include "llvm/Support/raw_ostream.h"
17 
18 void clang::DoRewriteTest(Preprocessor &PP, raw_ostream *OS) {
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(tok::comment)) continue;
28 
29  Rewriter.AddTokenBefore(I, "<i>");
30  Rewriter.AddTokenAfter(I, "</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(*I);
38 }
#define SM(sm)
Definition: Cuda.cpp:83
llvm::raw_ostream & OS
Definition: Logger.cpp:24
Defines the clang::Preprocessor interface.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:482
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
SourceManager & getSourceManager() const
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
const LangOptions & getLangOpts() const
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:32
This class handles loading and caching of source files into memory.
std::list< Token >::const_iterator token_iterator
Definition: TokenRewriter.h:56
void DoRewriteTest(Preprocessor &PP, raw_ostream *OS)
DoRewriteTest - A simple test for the TokenRewriter class.
Definition: RewriteTest.cpp:18