This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathTokenRewriter.cpp
53 lines (42 loc) · 1.75 KB
/
TokenRewriter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//===--- TokenRewriter.cpp - Token-based code rewriting interface ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the TokenRewriter class, which is used for code
// transformations.
//
//===----------------------------------------------------------------------===//
#include "clang/Rewrite/TokenRewriter.h"
#include "clang/Lex/Lexer.h"
#include "clang/Basic/SourceManager.h"
using namespace clang;
TokenRewriter::TokenRewriter(unsigned FileID, SourceManager &SM,
const LangOptions &LangOpts) {
std::pair<const char*,const char*> File = SM.getBufferData(FileID);
// Create a lexer to lex all the tokens of the main file in raw mode.
Lexer RawLex(SourceLocation::getFileLoc(FileID, 0),
LangOpts, File.first, File.second);
// Return all comments and whitespace as tokens.
RawLex.SetKeepWhitespaceMode(true);
// Lex the file, populating our datastructures.
Token RawTok;
RawLex.LexFromRawLexer(RawTok);
while (RawTok.isNot(tok::eof)) {
AddToken(RawTok, TokenList.end());
RawLex.LexFromRawLexer(RawTok);
}
}
/// AddToken - Add the specified token into the Rewriter before the other
/// position.
void TokenRewriter::AddToken(const Token &T, TokenRefTy Where) {
Where = TokenList.insert(Where, T);
bool InsertSuccess = TokenAtLoc.insert(std::make_pair(T.getLocation(),
Where)).second;
assert(InsertSuccess && "Token location already in rewriter!");
InsertSuccess = InsertSuccess;
}