xref: /freebsd/contrib/llvm-project/clang/include/clang/Rewrite/Core/TokenRewriter.h (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 
17 #include "clang/Basic/SourceLocation.h"
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.
50     TokenRewriter(FileID FID, SourceManager &SM, const LangOptions &LO);
51 
52     TokenRewriter(const TokenRewriter &) = delete;
53     TokenRewriter &operator=(const TokenRewriter &) = delete;
54     ~TokenRewriter();
55 
56     using token_iterator = std::list<Token>::const_iterator;
57 
token_begin()58     token_iterator token_begin() const { return TokenList.begin(); }
token_end()59     token_iterator token_end() const { return TokenList.end(); }
60 
61     token_iterator AddTokenBefore(token_iterator I, const char *Val);
62 
AddTokenAfter(token_iterator I,const char * Val)63     token_iterator AddTokenAfter(token_iterator I, const char *Val) {
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
81