18#include "llvm/ADT/RewriteBuffer.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/raw_ostream.h"
25using llvm::RewriteBuffer;
50 unsigned &CurTok,
bool ReturnComment) {
51 assert(CurTok < RawTokens.size() &&
"Overran eof!");
54 if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
57 return RawTokens[CurTok++];
64 std::vector<Token> &RawTokens) {
69 llvm::MemoryBufferRef FromFile =
SM.getBufferOrFake(
SM.getMainFileID());
82 if (RawTok.
is(tok::raw_identifier))
85 RawTokens.push_back(RawTok);
86 }
while (RawTok.
isNot(tok::eof));
96 RewriteBuffer &RB =
Rewrite.getEditBuffer(
SM.getMainFileID());
98 std::vector<Token> RawTokens;
100 unsigned CurRawTok = 0;
115 while (RawTok.
isNot(tok::eof) || PPTok.
isNot(tok::eof)) {
119 if (!
SM.isWrittenInMainFile(PPLoc)) {
131 if (RawTokens[CurRawTok].is(tok::identifier)) {
132 const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
133 if (II->
getName() ==
"warning") {
135 RB.InsertTextAfter(
SM.getFileOffset(RawTok.
getLocation()),
"//");
136 }
else if (II->
getName() ==
"pragma" &&
137 RawTokens[CurRawTok+1].is(tok::identifier) &&
138 (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
141 RB.InsertTextAfter(
SM.getFileOffset(RawTok.
getLocation()),
"//");
155 unsigned PPOffs =
SM.getFileOffset(PPLoc);
159 if (PPOffs == RawOffs &&
isSameToken(RawTok, PPTok)) {
167 if (RawOffs <= PPOffs) {
171 RB.InsertTextAfter(RawOffs, &
" /*"[HasSpace]);
180 if (RawTok.
is(tok::comment)) {
187 (PPOffs != RawOffs || !
isSameToken(RawTok, PPTok)));
189 RB.InsertTextBefore(EndPos,
"*/");
196 unsigned InsertPos = PPOffs;
197 std::string Expansion;
198 while (PPOffs < RawOffs) {
202 PPOffs =
SM.getFileOffset(PPLoc);
205 RB.InsertTextBefore(InsertPos, Expansion);
210 if (
const RewriteBuffer *RewriteBuf =
211 Rewrite.getRewriteBufferFor(
SM.getMainFileID())) {
213 *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
215 fprintf(stderr,
"No changes\n");
Defines the clang::Preprocessor interface.
static bool isSameToken(Token &RawTok, Token &PPTok)
isSameToken - Return true if the two specified tokens start have the same content.
static const Token & GetNextRawTok(const std::vector< Token > &RawTokens, unsigned &CurTok, bool ReturnComment)
GetNextRawTok - Return the next raw token in the stream, skipping over comments if ReturnComment is f...
static void LexRawTokensFromMainFile(Preprocessor &PP, std::vector< Token > &RawTokens)
LexRawTokensFromMainFile - Lets all the raw tokens from the main file into the specified vector.
Defines the SourceManager interface.
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.
bool LexFromRawLexer(Token &Result)
LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...
void SetCommentRetentionState(bool Mode)
SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void Lex(Token &Result)
Lex the next token for this preprocessor.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
SourceManager & getSourceManager() const
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
const LangOptions & getLangOpts() const
Rewriter - This is the main interface to the rewrite buffers.
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
IdentifierInfo * getIdentifierInfo() const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
unsigned getLength() const
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
tok::TokenKind getKind() const
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
bool isNot(tok::TokenKind K) const
The JSON file list parser is used to communicate input to InstallAPI.
@ Rewrite
We are substituting template parameters for (typically) other template parameters in order to rewrite...
void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS)
RewriteMacrosInInput - Implement -rewrite-macros mode.