clang  14.0.0git
TokenRewriter.h
Go to the documentation of this file.
1 //===- TokenRewriter.h - Token-based Rewriter -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the TokenRewriter class, which is used for code
10 // transformations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
15 #define LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
16 
18 #include "clang/Lex/Token.h"
19 #include <cassert>
20 #include <list>
21 #include <map>
22 #include <memory>
23 
24 namespace clang {
25 
26 class LangOptions;
27 class ScratchBuffer;
28 class SourceManager;
29 
30  class TokenRewriter {
31  /// TokenList - This is the list of raw tokens that make up this file. Each
32  /// of these tokens has a unique SourceLocation, which is a FileID.
33  std::list<Token> TokenList;
34 
35  /// TokenRefTy - This is the type used to refer to a token in the TokenList.
36  using TokenRefTy = std::list<Token>::iterator;
37 
38  /// TokenAtLoc - This map indicates which token exists at a specific
39  /// SourceLocation. Since each token has a unique SourceLocation, this is a
40  /// one to one map. The token can return its own location directly, to map
41  /// backwards.
42  std::map<SourceLocation, TokenRefTy> TokenAtLoc;
43 
44  /// ScratchBuf - This is the buffer that we create scratch tokens from.
45  std::unique_ptr<ScratchBuffer> ScratchBuf;
46 
47  public:
48  /// TokenRewriter - This creates a TokenRewriter for the file with the
49  /// specified FileID.
51 
52  TokenRewriter(const TokenRewriter &) = delete;
53  TokenRewriter &operator=(const TokenRewriter &) = delete;
55 
56  using token_iterator = std::list<Token>::const_iterator;
57 
58  token_iterator token_begin() const { return TokenList.begin(); }
59  token_iterator token_end() const { return TokenList.end(); }
60 
61  token_iterator AddTokenBefore(token_iterator I, const char *Val);
62 
64  assert(I != token_end() && "Cannot insert after token_end()!");
65  return AddTokenBefore(++I, Val);
66  }
67 
68  private:
69  /// RemapIterator - Convert from token_iterator (a const iterator) to
70  /// TokenRefTy (a non-const iterator).
71  TokenRefTy RemapIterator(token_iterator I);
72 
73  /// AddToken - Add the specified token into the Rewriter before the other
74  /// position.
75  TokenRefTy AddToken(const Token &T, TokenRefTy Where);
76  };
77 
78 } // namespace clang
79 
80 #endif // LLVM_CLANG_REWRITE_CORE_TOKENREWRITER_H
clang::TokenRewriter::token_iterator
std::list< Token >::const_iterator token_iterator
Definition: TokenRewriter.h:56
clang::TokenRewriter::AddTokenBefore
token_iterator AddTokenBefore(token_iterator I, const char *Val)
Definition: TokenRewriter.cpp:83
clang::TokenRewriter
Definition: TokenRewriter.h:30
clang::TokenRewriter::TokenRewriter
TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO)
TokenRewriter - This creates a TokenRewriter for the file with the specified FileID.
Definition: TokenRewriter.cpp:26
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
SourceLocation.h
clang::TokenRewriter::token_end
token_iterator token_end() const
Definition: TokenRewriter.h:59
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:58
clang::TokenRewriter::token_begin
token_iterator token_begin() const
Definition: TokenRewriter.h:58
clang::TokenRewriter::~TokenRewriter
~TokenRewriter()
clang::TokenRewriter::operator=
TokenRewriter & operator=(const TokenRewriter &)=delete
clang
Definition: CalledOnceCheck.h:17
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
SM
#define SM(sm)
Definition: Cuda.cpp:78
Token.h
clang::TokenRewriter::AddTokenAfter
token_iterator AddTokenAfter(token_iterator I, const char *Val)
Definition: TokenRewriter.h:63