1e5dd7070Spatrick //===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This code rewrites macro invocations into their expansions. This gives you
10e5dd7070Spatrick // a macro expanded file that retains comments and #includes.
11e5dd7070Spatrick //
12e5dd7070Spatrick //===----------------------------------------------------------------------===//
13e5dd7070Spatrick
14e5dd7070Spatrick #include "clang/Rewrite/Frontend/Rewriters.h"
15e5dd7070Spatrick #include "clang/Basic/SourceManager.h"
16e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
17e5dd7070Spatrick #include "clang/Rewrite/Core/Rewriter.h"
18e5dd7070Spatrick #include "llvm/Support/Path.h"
19e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
20e5dd7070Spatrick #include <cstdio>
21e5dd7070Spatrick #include <memory>
22e5dd7070Spatrick
23e5dd7070Spatrick using namespace clang;
24e5dd7070Spatrick
25e5dd7070Spatrick /// isSameToken - Return true if the two specified tokens start have the same
26e5dd7070Spatrick /// content.
isSameToken(Token & RawTok,Token & PPTok)27e5dd7070Spatrick static bool isSameToken(Token &RawTok, Token &PPTok) {
28e5dd7070Spatrick // If two tokens have the same kind and the same identifier info, they are
29e5dd7070Spatrick // obviously the same.
30e5dd7070Spatrick if (PPTok.getKind() == RawTok.getKind() &&
31e5dd7070Spatrick PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
32e5dd7070Spatrick return true;
33e5dd7070Spatrick
34e5dd7070Spatrick // Otherwise, if they are different but have the same identifier info, they
35e5dd7070Spatrick // are also considered to be the same. This allows keywords and raw lexed
36e5dd7070Spatrick // identifiers with the same name to be treated the same.
37e5dd7070Spatrick if (PPTok.getIdentifierInfo() &&
38e5dd7070Spatrick PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo())
39e5dd7070Spatrick return true;
40e5dd7070Spatrick
41e5dd7070Spatrick return false;
42e5dd7070Spatrick }
43e5dd7070Spatrick
44e5dd7070Spatrick
45e5dd7070Spatrick /// GetNextRawTok - Return the next raw token in the stream, skipping over
46e5dd7070Spatrick /// comments if ReturnComment is false.
GetNextRawTok(const std::vector<Token> & RawTokens,unsigned & CurTok,bool ReturnComment)47e5dd7070Spatrick static const Token &GetNextRawTok(const std::vector<Token> &RawTokens,
48e5dd7070Spatrick unsigned &CurTok, bool ReturnComment) {
49e5dd7070Spatrick assert(CurTok < RawTokens.size() && "Overran eof!");
50e5dd7070Spatrick
51e5dd7070Spatrick // If the client doesn't want comments and we have one, skip it.
52e5dd7070Spatrick if (!ReturnComment && RawTokens[CurTok].is(tok::comment))
53e5dd7070Spatrick ++CurTok;
54e5dd7070Spatrick
55e5dd7070Spatrick return RawTokens[CurTok++];
56e5dd7070Spatrick }
57e5dd7070Spatrick
58e5dd7070Spatrick
59e5dd7070Spatrick /// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into
60e5dd7070Spatrick /// the specified vector.
LexRawTokensFromMainFile(Preprocessor & PP,std::vector<Token> & RawTokens)61e5dd7070Spatrick static void LexRawTokensFromMainFile(Preprocessor &PP,
62e5dd7070Spatrick std::vector<Token> &RawTokens) {
63e5dd7070Spatrick SourceManager &SM = PP.getSourceManager();
64e5dd7070Spatrick
65e5dd7070Spatrick // Create a lexer to lex all the tokens of the main file in raw mode. Even
66e5dd7070Spatrick // though it is in raw mode, it will not return comments.
67*a9ac8606Spatrick llvm::MemoryBufferRef FromFile = SM.getBufferOrFake(SM.getMainFileID());
68e5dd7070Spatrick Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
69e5dd7070Spatrick
70e5dd7070Spatrick // Switch on comment lexing because we really do want them.
71e5dd7070Spatrick RawLex.SetCommentRetentionState(true);
72e5dd7070Spatrick
73e5dd7070Spatrick Token RawTok;
74e5dd7070Spatrick do {
75e5dd7070Spatrick RawLex.LexFromRawLexer(RawTok);
76e5dd7070Spatrick
77e5dd7070Spatrick // If we have an identifier with no identifier info for our raw token, look
78e5dd7070Spatrick // up the identifier info. This is important for equality comparison of
79e5dd7070Spatrick // identifier tokens.
80e5dd7070Spatrick if (RawTok.is(tok::raw_identifier))
81e5dd7070Spatrick PP.LookUpIdentifierInfo(RawTok);
82e5dd7070Spatrick
83e5dd7070Spatrick RawTokens.push_back(RawTok);
84e5dd7070Spatrick } while (RawTok.isNot(tok::eof));
85e5dd7070Spatrick }
86e5dd7070Spatrick
87e5dd7070Spatrick
88e5dd7070Spatrick /// RewriteMacrosInInput - Implement -rewrite-macros mode.
RewriteMacrosInInput(Preprocessor & PP,raw_ostream * OS)89e5dd7070Spatrick void clang::RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS) {
90e5dd7070Spatrick SourceManager &SM = PP.getSourceManager();
91e5dd7070Spatrick
92e5dd7070Spatrick Rewriter Rewrite;
93e5dd7070Spatrick Rewrite.setSourceMgr(SM, PP.getLangOpts());
94e5dd7070Spatrick RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
95e5dd7070Spatrick
96e5dd7070Spatrick std::vector<Token> RawTokens;
97e5dd7070Spatrick LexRawTokensFromMainFile(PP, RawTokens);
98e5dd7070Spatrick unsigned CurRawTok = 0;
99e5dd7070Spatrick Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
100e5dd7070Spatrick
101e5dd7070Spatrick
102e5dd7070Spatrick // Get the first preprocessing token.
103e5dd7070Spatrick PP.EnterMainSourceFile();
104e5dd7070Spatrick Token PPTok;
105e5dd7070Spatrick PP.Lex(PPTok);
106e5dd7070Spatrick
107e5dd7070Spatrick // Preprocess the input file in parallel with raw lexing the main file. Ignore
108e5dd7070Spatrick // all tokens that are preprocessed from a file other than the main file (e.g.
109e5dd7070Spatrick // a header). If we see tokens that are in the preprocessed file but not the
110e5dd7070Spatrick // lexed file, we have a macro expansion. If we see tokens in the lexed file
111e5dd7070Spatrick // that aren't in the preprocessed view, we have macros that expand to no
112e5dd7070Spatrick // tokens, or macro arguments etc.
113e5dd7070Spatrick while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) {
114e5dd7070Spatrick SourceLocation PPLoc = SM.getExpansionLoc(PPTok.getLocation());
115e5dd7070Spatrick
116e5dd7070Spatrick // If PPTok is from a different source file, ignore it.
117e5dd7070Spatrick if (!SM.isWrittenInMainFile(PPLoc)) {
118e5dd7070Spatrick PP.Lex(PPTok);
119e5dd7070Spatrick continue;
120e5dd7070Spatrick }
121e5dd7070Spatrick
122e5dd7070Spatrick // If the raw file hits a preprocessor directive, they will be extra tokens
123e5dd7070Spatrick // in the raw file that don't exist in the preprocsesed file. However, we
124e5dd7070Spatrick // choose to preserve them in the output file and otherwise handle them
125e5dd7070Spatrick // specially.
126e5dd7070Spatrick if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) {
127e5dd7070Spatrick // If this is a #warning directive or #pragma mark (GNU extensions),
128e5dd7070Spatrick // comment the line out.
129e5dd7070Spatrick if (RawTokens[CurRawTok].is(tok::identifier)) {
130e5dd7070Spatrick const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo();
131e5dd7070Spatrick if (II->getName() == "warning") {
132e5dd7070Spatrick // Comment out #warning.
133e5dd7070Spatrick RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
134e5dd7070Spatrick } else if (II->getName() == "pragma" &&
135e5dd7070Spatrick RawTokens[CurRawTok+1].is(tok::identifier) &&
136e5dd7070Spatrick (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() ==
137e5dd7070Spatrick "mark")) {
138e5dd7070Spatrick // Comment out #pragma mark.
139e5dd7070Spatrick RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//");
140e5dd7070Spatrick }
141e5dd7070Spatrick }
142e5dd7070Spatrick
143e5dd7070Spatrick // Otherwise, if this is a #include or some other directive, just leave it
144e5dd7070Spatrick // in the file by skipping over the line.
145e5dd7070Spatrick RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
146e5dd7070Spatrick while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof))
147e5dd7070Spatrick RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
148e5dd7070Spatrick continue;
149e5dd7070Spatrick }
150e5dd7070Spatrick
151e5dd7070Spatrick // Okay, both tokens are from the same file. Get their offsets from the
152e5dd7070Spatrick // start of the file.
153e5dd7070Spatrick unsigned PPOffs = SM.getFileOffset(PPLoc);
154e5dd7070Spatrick unsigned RawOffs = SM.getFileOffset(RawTok.getLocation());
155e5dd7070Spatrick
156e5dd7070Spatrick // If the offsets are the same and the token kind is the same, ignore them.
157e5dd7070Spatrick if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) {
158e5dd7070Spatrick RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
159e5dd7070Spatrick PP.Lex(PPTok);
160e5dd7070Spatrick continue;
161e5dd7070Spatrick }
162e5dd7070Spatrick
163e5dd7070Spatrick // If the PP token is farther along than the raw token, something was
164e5dd7070Spatrick // deleted. Comment out the raw token.
165e5dd7070Spatrick if (RawOffs <= PPOffs) {
166e5dd7070Spatrick // Comment out a whole run of tokens instead of bracketing each one with
167e5dd7070Spatrick // comments. Add a leading space if RawTok didn't have one.
168e5dd7070Spatrick bool HasSpace = RawTok.hasLeadingSpace();
169e5dd7070Spatrick RB.InsertTextAfter(RawOffs, &" /*"[HasSpace]);
170e5dd7070Spatrick unsigned EndPos;
171e5dd7070Spatrick
172e5dd7070Spatrick do {
173e5dd7070Spatrick EndPos = RawOffs+RawTok.getLength();
174e5dd7070Spatrick
175e5dd7070Spatrick RawTok = GetNextRawTok(RawTokens, CurRawTok, true);
176e5dd7070Spatrick RawOffs = SM.getFileOffset(RawTok.getLocation());
177e5dd7070Spatrick
178e5dd7070Spatrick if (RawTok.is(tok::comment)) {
179e5dd7070Spatrick // Skip past the comment.
180e5dd7070Spatrick RawTok = GetNextRawTok(RawTokens, CurRawTok, false);
181e5dd7070Spatrick break;
182e5dd7070Spatrick }
183e5dd7070Spatrick
184e5dd7070Spatrick } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() &&
185e5dd7070Spatrick (PPOffs != RawOffs || !isSameToken(RawTok, PPTok)));
186e5dd7070Spatrick
187e5dd7070Spatrick RB.InsertTextBefore(EndPos, "*/");
188e5dd7070Spatrick continue;
189e5dd7070Spatrick }
190e5dd7070Spatrick
191e5dd7070Spatrick // Otherwise, there was a replacement an expansion. Insert the new token
192e5dd7070Spatrick // in the output buffer. Insert the whole run of new tokens at once to get
193e5dd7070Spatrick // them in the right order.
194e5dd7070Spatrick unsigned InsertPos = PPOffs;
195e5dd7070Spatrick std::string Expansion;
196e5dd7070Spatrick while (PPOffs < RawOffs) {
197e5dd7070Spatrick Expansion += ' ' + PP.getSpelling(PPTok);
198e5dd7070Spatrick PP.Lex(PPTok);
199e5dd7070Spatrick PPLoc = SM.getExpansionLoc(PPTok.getLocation());
200e5dd7070Spatrick PPOffs = SM.getFileOffset(PPLoc);
201e5dd7070Spatrick }
202e5dd7070Spatrick Expansion += ' ';
203e5dd7070Spatrick RB.InsertTextBefore(InsertPos, Expansion);
204e5dd7070Spatrick }
205e5dd7070Spatrick
206e5dd7070Spatrick // Get the buffer corresponding to MainFileID. If we haven't changed it, then
207e5dd7070Spatrick // we are done.
208e5dd7070Spatrick if (const RewriteBuffer *RewriteBuf =
209e5dd7070Spatrick Rewrite.getRewriteBufferFor(SM.getMainFileID())) {
210e5dd7070Spatrick //printf("Changed:\n");
211e5dd7070Spatrick *OS << std::string(RewriteBuf->begin(), RewriteBuf->end());
212e5dd7070Spatrick } else {
213e5dd7070Spatrick fprintf(stderr, "No changes\n");
214e5dd7070Spatrick }
215e5dd7070Spatrick OS->flush();
216e5dd7070Spatrick }
217