clang 8.0.0

RewriteTest.cpp
Go to the documentation of this file.
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
14 #include "clang/Lex/Preprocessor.h"
15 #include "clang/Rewrite/Core/TokenRewriter.h"
16 #include "clang/Rewrite/Frontend/Rewriters.h"
17 #include "llvm/Support/raw_ostream.h"
18
19 void clang::DoRewriteTest(Preprocessor &PP, raw_ostream *OS) {
20 SourceManager &SM = PP.getSourceManager();
21 const LangOptions &LangOpts = PP.getLangOpts();
22
23 TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts);
24
25 // Throw <i> </i> tags around comments.
26 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
27 E = Rewriter.token_end(); I != E; ++I) {
28 if (I->isNot(tok::comment)) continue;
29
30 Rewriter.AddTokenBefore(I, "<i>");
31 Rewriter.AddTokenAfter(I, "</i>");
32 }
33
34
35 // Print out the output.
36 for (TokenRewriter::token_iterator I = Rewriter.token_begin(),
37 E = Rewriter.token_end(); I != E; ++I)
38 *OS << PP.getSpelling(*I);
39 }
Rewriters.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
clang::Preprocessor::getLangOpts
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:815
clang::Preprocessor::getSpelling
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the &#39;spelling&#39; of the token at the given location; does not go up to the spelling location or ...
Definition: Preprocessor.h:1564
clang::TokenRewriter
Definition: TokenRewriter.h:31
TokenRewriter.h
Preprocessor.h
Defines the clang::Preprocessor interface.
SM
const SourceManager & SM
Definition: Format.cpp:1490
clang::Preprocessor::getSourceManager
SourceManager & getSourceManager() const
Definition: Preprocessor.h:819
clang::TokenRewriter::token_iterator
std::list< Token >::const_iterator token_iterator
Definition: TokenRewriter.h:57
clang::SourceManager::getMainFileID
FileID getMainFileID() const
Returns the FileID of the main source file.
Definition: SourceManager.h:793
clang::DoRewriteTest
void DoRewriteTest(Preprocessor &PP, raw_ostream *OS)
DoRewriteTest - A simple test for the TokenRewriter class.
Definition: RewriteTest.cpp:19
clang::Rewriter
Rewriter - This is the main interface to the rewrite buffers.
Definition: Rewriter.h:33
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:582
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:125