1*0a6a1f1dSLionel Sambuc //===--- TokenRewriter.cpp - Token-based code rewriting interface ---------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc //  This file implements the TokenRewriter class, which is used for code
11*0a6a1f1dSLionel Sambuc //  transformations.
12*0a6a1f1dSLionel Sambuc //
13*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Core/TokenRewriter.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Basic/SourceManager.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Lex/Lexer.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Lex/ScratchBuffer.h"
19*0a6a1f1dSLionel Sambuc using namespace clang;
20*0a6a1f1dSLionel Sambuc 
TokenRewriter(FileID FID,SourceManager & SM,const LangOptions & LangOpts)21*0a6a1f1dSLionel Sambuc TokenRewriter::TokenRewriter(FileID FID, SourceManager &SM,
22*0a6a1f1dSLionel Sambuc                              const LangOptions &LangOpts) {
23*0a6a1f1dSLionel Sambuc   ScratchBuf.reset(new ScratchBuffer(SM));
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc   // Create a lexer to lex all the tokens of the main file in raw mode.
26*0a6a1f1dSLionel Sambuc   const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
27*0a6a1f1dSLionel Sambuc   Lexer RawLex(FID, FromFile, SM, LangOpts);
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc   // Return all comments and whitespace as tokens.
30*0a6a1f1dSLionel Sambuc   RawLex.SetKeepWhitespaceMode(true);
31*0a6a1f1dSLionel Sambuc 
32*0a6a1f1dSLionel Sambuc   // Lex the file, populating our datastructures.
33*0a6a1f1dSLionel Sambuc   Token RawTok;
34*0a6a1f1dSLionel Sambuc   RawLex.LexFromRawLexer(RawTok);
35*0a6a1f1dSLionel Sambuc   while (RawTok.isNot(tok::eof)) {
36*0a6a1f1dSLionel Sambuc #if 0
37*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::raw_identifier)) {
38*0a6a1f1dSLionel Sambuc       // Look up the identifier info for the token.  This should use
39*0a6a1f1dSLionel Sambuc       // IdentifierTable directly instead of PP.
40*0a6a1f1dSLionel Sambuc       PP.LookUpIdentifierInfo(Tok);
41*0a6a1f1dSLionel Sambuc     }
42*0a6a1f1dSLionel Sambuc #endif
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc     AddToken(RawTok, TokenList.end());
45*0a6a1f1dSLionel Sambuc     RawLex.LexFromRawLexer(RawTok);
46*0a6a1f1dSLionel Sambuc   }
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc 
~TokenRewriter()49*0a6a1f1dSLionel Sambuc TokenRewriter::~TokenRewriter() {
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc 
53*0a6a1f1dSLionel Sambuc /// RemapIterator - Convert from token_iterator (a const iterator) to
54*0a6a1f1dSLionel Sambuc /// TokenRefTy (a non-const iterator).
RemapIterator(token_iterator I)55*0a6a1f1dSLionel Sambuc TokenRewriter::TokenRefTy TokenRewriter::RemapIterator(token_iterator I) {
56*0a6a1f1dSLionel Sambuc   if (I == token_end()) return TokenList.end();
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc   // FIXME: This is horrible, we should use our own list or something to avoid
59*0a6a1f1dSLionel Sambuc   // this.
60*0a6a1f1dSLionel Sambuc   std::map<SourceLocation, TokenRefTy>::iterator MapIt =
61*0a6a1f1dSLionel Sambuc     TokenAtLoc.find(I->getLocation());
62*0a6a1f1dSLionel Sambuc   assert(MapIt != TokenAtLoc.end() && "iterator not in rewriter?");
63*0a6a1f1dSLionel Sambuc   return MapIt->second;
64*0a6a1f1dSLionel Sambuc }
65*0a6a1f1dSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc /// AddToken - Add the specified token into the Rewriter before the other
68*0a6a1f1dSLionel Sambuc /// position.
69*0a6a1f1dSLionel Sambuc TokenRewriter::TokenRefTy
AddToken(const Token & T,TokenRefTy Where)70*0a6a1f1dSLionel Sambuc TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
71*0a6a1f1dSLionel Sambuc   Where = TokenList.insert(Where, T);
72*0a6a1f1dSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
74*0a6a1f1dSLionel Sambuc                                                         Where)).second;
75*0a6a1f1dSLionel Sambuc   assert(InsertSuccess && "Token location already in rewriter!");
76*0a6a1f1dSLionel Sambuc   (void)InsertSuccess;
77*0a6a1f1dSLionel Sambuc   return Where;
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc TokenRewriter::token_iterator
AddTokenBefore(token_iterator I,const char * Val)82*0a6a1f1dSLionel Sambuc TokenRewriter::AddTokenBefore(token_iterator I, const char *Val) {
83*0a6a1f1dSLionel Sambuc   unsigned Len = strlen(Val);
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc   // Plop the string into the scratch buffer, then create a token for this
86*0a6a1f1dSLionel Sambuc   // string.
87*0a6a1f1dSLionel Sambuc   Token Tok;
88*0a6a1f1dSLionel Sambuc   Tok.startToken();
89*0a6a1f1dSLionel Sambuc   const char *Spelling;
90*0a6a1f1dSLionel Sambuc   Tok.setLocation(ScratchBuf->getToken(Val, Len, Spelling));
91*0a6a1f1dSLionel Sambuc   Tok.setLength(Len);
92*0a6a1f1dSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc   // TODO: Form a whole lexer around this and relex the token!  For now, just
94*0a6a1f1dSLionel Sambuc   // set kind to tok::unknown.
95*0a6a1f1dSLionel Sambuc   Tok.setKind(tok::unknown);
96*0a6a1f1dSLionel Sambuc 
97*0a6a1f1dSLionel Sambuc   return AddToken(Tok, RemapIterator(I));
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc 
100