clang  6.0.0svn
TokenRewriter.h
Go to the documentation of this file.
1 //===--- TokenRewriter.h - Token-based Rewriter -----------------*- C++ -*-===//
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 defines the TokenRewriter class, which is used for code
11 // transformations.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
16 #define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
17 
19 #include "clang/Lex/Token.h"
20 #include <list>
21 #include <map>
22 #include <memory>
23 
24 namespace clang {
25  class LangOptions;
26  class ScratchBuffer;
27 
28  class TokenRewriter {
29  /// TokenList - This is the list of raw tokens that make up this file. Each
30  /// of these tokens has a unique SourceLocation, which is a FileID.
31  std::list<Token> TokenList;
32 
33  /// TokenRefTy - This is the type used to refer to a token in the TokenList.
34  typedef std::list<Token>::iterator TokenRefTy;
35 
36  /// TokenAtLoc - This map indicates which token exists at a specific
37  /// SourceLocation. Since each token has a unique SourceLocation, this is a
38  /// one to one map. The token can return its own location directly, to map
39  /// backwards.
40  std::map<SourceLocation, TokenRefTy> TokenAtLoc;
41 
42  /// ScratchBuf - This is the buffer that we create scratch tokens from.
43  ///
44  std::unique_ptr<ScratchBuffer> ScratchBuf;
45 
46  TokenRewriter(const TokenRewriter &) = delete;
47  void operator=(const TokenRewriter &) = delete;
48  public:
49  /// TokenRewriter - This creates a TokenRewriter for the file with the
50  /// specified FileID.
53 
54  typedef std::list<Token>::const_iterator token_iterator;
55  token_iterator token_begin() const { return TokenList.begin(); }
56  token_iterator token_end() const { return TokenList.end(); }
57 
58 
59  token_iterator AddTokenBefore(token_iterator I, const char *Val);
60  token_iterator AddTokenAfter(token_iterator I, const char *Val) {
61  assert(I != token_end() && "Cannot insert after token_end()!");
62  return AddTokenBefore(++I, Val);
63  }
64 
65  private:
66  /// RemapIterator - Convert from token_iterator (a const iterator) to
67  /// TokenRefTy (a non-const iterator).
68  TokenRefTy RemapIterator(token_iterator I);
69 
70  /// AddToken - Add the specified token into the Rewriter before the other
71  /// position.
72  TokenRefTy AddToken(const Token &T, TokenRefTy Where);
73  };
74 
75 
76 
77 } // end namespace clang
78 
79 #endif
token_iterator token_end() const
Definition: TokenRewriter.h:56
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
token_iterator token_begin() const
Definition: TokenRewriter.h:55
token_iterator AddTokenAfter(token_iterator I, const char *Val)
Definition: TokenRewriter.h:60
std::list< Token >::const_iterator token_iterator
Definition: TokenRewriter.h:54
const FunctionProtoType * T
const SourceManager & SM
Definition: Format.cpp:1337
token_iterator AddTokenBefore(token_iterator I, const char *Val)
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
Defines the clang::SourceLocation class and associated facilities.
This class handles loading and caching of source files into memory.