clang 8.0.0

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