zcov: / lib/Rewrite/TokenRewriter.cpp


Files: 1 Branches Taken: 0.0% 0 / 10
Generated: 2010-02-10 01:31 Branches Executed: 0.0% 0 / 10
Line Coverage: 0.0% 0 / 33


Programs: 1 Runs 2897


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

Generated: 2010-02-10 01:31 by zcov