clang API Documentation

TokenRewriter.h
Go to the documentation of this file.
00001 //===--- TokenRewriter.h - Token-based Rewriter -----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the TokenRewriter class, which is used for code
00011 //  transformations.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
00016 #define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
00017 
00018 #include "clang/Basic/SourceLocation.h"
00019 #include "clang/Lex/Token.h"
00020 #include <list>
00021 #include <map>
00022 #include <memory>
00023 
00024 namespace clang {
00025   class LangOptions;
00026   class ScratchBuffer;
00027 
00028   class TokenRewriter {
00029     /// TokenList - This is the list of raw tokens that make up this file.  Each
00030     /// of these tokens has a unique SourceLocation, which is a FileID.
00031     std::list<Token> TokenList;
00032 
00033     /// TokenRefTy - This is the type used to refer to a token in the TokenList.
00034     typedef std::list<Token>::iterator TokenRefTy;
00035 
00036     /// TokenAtLoc - This map indicates which token exists at a specific
00037     /// SourceLocation.  Since each token has a unique SourceLocation, this is a
00038     /// one to one map.  The token can return its own location directly, to map
00039     /// backwards.
00040     std::map<SourceLocation, TokenRefTy> TokenAtLoc;
00041 
00042     /// ScratchBuf - This is the buffer that we create scratch tokens from.
00043     ///
00044     std::unique_ptr<ScratchBuffer> ScratchBuf;
00045 
00046     TokenRewriter(const TokenRewriter &) LLVM_DELETED_FUNCTION;
00047     void operator=(const TokenRewriter &) LLVM_DELETED_FUNCTION;
00048   public:
00049     /// TokenRewriter - This creates a TokenRewriter for the file with the
00050     /// specified FileID.
00051     TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO);
00052     ~TokenRewriter();
00053 
00054     typedef std::list<Token>::const_iterator token_iterator;
00055     token_iterator token_begin() const { return TokenList.begin(); }
00056     token_iterator token_end() const { return TokenList.end(); }
00057 
00058 
00059     token_iterator AddTokenBefore(token_iterator I, const char *Val);
00060     token_iterator AddTokenAfter(token_iterator I, const char *Val) {
00061       assert(I != token_end() && "Cannot insert after token_end()!");
00062       return AddTokenBefore(++I, Val);
00063     }
00064 
00065   private:
00066     /// RemapIterator - Convert from token_iterator (a const iterator) to
00067     /// TokenRefTy (a non-const iterator).
00068     TokenRefTy RemapIterator(token_iterator I);
00069 
00070     /// AddToken - Add the specified token into the Rewriter before the other
00071     /// position.
00072     TokenRefTy AddToken(const Token &T, TokenRefTy Where);
00073   };
00074 
00075 
00076 
00077 } // end namespace clang
00078 
00079 #endif