blob: b55df3d652b0d5c92210ecb9f5c3f92c669afa48 [file] [log] [blame]
Chris Lattner09e3cdf2006-07-04 19:04:05 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This code simply runs the preprocessor on the input file and prints out the
11// result. This is the traditional behavior of the -E option.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang.h"
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +000016#include "clang/Lex/PPCallbacks.h"
Chris Lattner09e3cdf2006-07-04 19:04:05 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Lex/Pragma.h"
19#include "clang/Basic/SourceManager.h"
20#include "llvm/Support/CommandLine.h"
Chris Lattnerf46be6c2006-07-04 22:19:33 +000021#include "llvm/ADT/StringExtras.h"
22#include "llvm/Config/config.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000023#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000024using namespace llvm;
25using namespace clang;
26
Chris Lattnerf46be6c2006-07-04 22:19:33 +000027//===----------------------------------------------------------------------===//
28// Simple buffered I/O
29//===----------------------------------------------------------------------===//
30//
31// Empirically, iostream is over 30% slower than stdio for this workload, and
32// stdio itself isn't very well suited. The problem with stdio is use of
33// putchar_unlocked. We have many newline characters that need to be emitted,
34// but stdio needs to do extra checks to handle line buffering mode. These
35// extra checks make putchar_unlocked fall off its inlined code path, hitting
36// slow system code. In practice, using 'write' directly makes 'clang -E -P'
37// about 10% faster than using the stdio path on darwin.
38
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#else
42#define USE_STDIO 1
43#endif
44
45static char *OutBufStart = 0, *OutBufEnd, *OutBufCur;
46
47/// InitOutputBuffer - Initialize our output buffer.
48///
49static void InitOutputBuffer() {
50#ifndef USE_STDIO
51 OutBufStart = new char[64*1024];
52 OutBufEnd = OutBufStart+64*1024;
53 OutBufCur = OutBufStart;
54#endif
55}
56
57/// FlushBuffer - Write the accumulated bytes to the output stream.
58///
59static void FlushBuffer() {
60#ifndef USE_STDIO
61 write(STDOUT_FILENO, OutBufStart, OutBufCur-OutBufStart);
62 OutBufCur = OutBufStart;
63#endif
64}
65
66/// CleanupOutputBuffer - Finish up output.
67///
68static void CleanupOutputBuffer() {
69#ifndef USE_STDIO
70 FlushBuffer();
71 delete [] OutBufStart;
72#endif
73}
74
75static void OutputChar(char c) {
76#ifdef USE_STDIO
77 putchar_unlocked(c);
78#else
79 if (OutBufCur >= OutBufEnd)
80 FlushBuffer();
81 *OutBufCur++ = c;
82#endif
83}
84
85static void OutputString(const char *Ptr, unsigned Size) {
86#ifdef USE_STDIO
87 fwrite(Ptr, Size, 1, stdout);
88#else
89 if (OutBufCur+Size >= OutBufEnd)
90 FlushBuffer();
91 memcpy(OutBufCur, Ptr, Size);
92 OutBufCur += Size;
93#endif
94}
95
96
97//===----------------------------------------------------------------------===//
98// Preprocessed token printer
99//===----------------------------------------------------------------------===//
100
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000101static cl::opt<bool>
102DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
Chris Lattner457fc152006-07-29 06:30:25 +0000103static cl::opt<bool>
104EnableCommentOutput("C", cl::desc("Enable comment output in -E mode"));
105static cl::opt<bool>
106EnableMacroCommentOutput("CC", cl::desc("Enable comment output in -E mode, "
107 "even from macro expansions"));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000108
109static unsigned EModeCurLine;
110static std::string EModeCurFilename;
111static Preprocessor *EModePP;
112static bool EmodeEmittedTokensOnThisLine;
113static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
114
Chris Lattner728b4dc2006-07-04 21:28:37 +0000115/// MoveToLine - Move the output to the source line specified by the location
116/// object. We can do this by emitting some number of \n's, or be emitting a
117/// #line directive.
Chris Lattner3338ba82006-07-04 21:19:39 +0000118static void MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000119 if (DisableLineMarkers) {
120 if (EmodeEmittedTokensOnThisLine) {
121 OutputChar('\n');
122 EmodeEmittedTokensOnThisLine = false;
123 }
124 return;
125 }
Chris Lattner3338ba82006-07-04 21:19:39 +0000126
127 unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc);
128
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000129 // If this line is "close enough" to the original line, just print newlines,
130 // otherwise print a #line directive.
131 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000132 unsigned CurLine = EModeCurLine;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000133 for (; CurLine != LineNo; ++CurLine)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000134 OutputChar('\n');
Chris Lattnerdeb37012006-07-04 19:24:06 +0000135 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000136 } else {
137 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000138 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000139 EmodeEmittedTokensOnThisLine = false;
140 }
141
142 EModeCurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000143
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000144 OutputChar('#');
145 OutputChar(' ');
146 std::string Num = utostr_32(LineNo);
147 OutputString(&Num[0], Num.size());
148 OutputChar(' ');
149 OutputString(&EModeCurFilename[0], EModeCurFilename.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000150
151 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000152 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000153 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000154 OutputString(" 3 4", 4);
155 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000156 }
157}
158
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000159namespace {
160class PrintPPOutputPPCallbacks : public PPCallbacks {
161public:
162 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
163 DirectoryLookup::DirType FileType);
164 virtual void Ident(SourceLocation Loc, const std::string &str);
165};
166}
167
168/// FileChanged - Whenever the preprocessor enters or exits a #include file
169/// it invokes this handler. Update our conception of the current source
170/// position.
171
172void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
173 FileChangeReason Reason,
174 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000175 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000176
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000177 // Unless we are exiting a #include, make sure to skip ahead to the line the
178 // #include directive was at.
Chris Lattnerff3f5f42006-07-04 21:25:59 +0000179 SourceManager &SourceMgr = EModePP->getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000180 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000181 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000182 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000183 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000184
185 // TODO GCC emits the # directive for this directive on the line AFTER the
186 // directive and emits a bunch of spaces that aren't needed. Emulate this
187 // strange behavior.
188 }
189
190 EModeCurLine = SourceMgr.getLineNumber(Loc);
Chris Lattnerecc39e92006-07-15 05:23:31 +0000191 EModeCurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000192 EmodeFileType = FileType;
193
194 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000195 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000196 EmodeEmittedTokensOnThisLine = false;
197 }
198
199 if (DisableLineMarkers) return;
200
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000201 OutputChar('#');
202 OutputChar(' ');
203 std::string Num = utostr_32(EModeCurLine);
204 OutputString(&Num[0], Num.size());
205 OutputChar(' ');
206 OutputString(&EModeCurFilename[0], EModeCurFilename.size());
207
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000208 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000209 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000210 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000211 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000212 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000213 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000214 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000215 case PPCallbacks::SystemHeaderPragma: break;
216 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000217 }
218
219 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000220 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000221 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000222 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000223
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000224 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000225}
226
Chris Lattner728b4dc2006-07-04 21:28:37 +0000227/// HandleIdent - Handle #ident directives when read by the preprocessor.
228///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000229void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000230 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000231
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000232 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000233 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000234 EmodeEmittedTokensOnThisLine = true;
235}
236
237/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
238/// is called for the first token on each new line.
239static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
240 // Figure out what line we went to and insert the appropriate number of
241 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000242 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000243
244 // Print out space characters so that the first token on a line is
245 // indented for easy reading.
246 unsigned ColNo =
247 PP.getSourceManager().getColumnNumber(Tok.getLocation());
248
249 // This hack prevents stuff like:
250 // #define HASH #
251 // HASH define foo bar
252 // From having the # character end up at column 1, which makes it so it
253 // is not handled as a #define next time through the preprocessor if in
254 // -fpreprocessed mode.
255 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000256 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000257
258 // Otherwise, indent the appropriate number of spaces.
259 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000260 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000261}
262
Chris Lattner5de858c2006-07-04 19:04:44 +0000263namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000264struct UnknownPragmaHandler : public PragmaHandler {
265 const char *Prefix;
266 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
267 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
268 // Figure out what line we went to and insert the appropriate number of
269 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000270 MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000271 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000272
273 // Read and print all of the pragma tokens.
274 while (PragmaTok.getKind() != tok::eom) {
275 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000276 OutputChar(' ');
277 std::string TokSpell = PP.getSpelling(PragmaTok);
278 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000279 PP.LexUnexpandedToken(PragmaTok);
280 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000281 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000282 }
283};
Chris Lattner5de858c2006-07-04 19:04:44 +0000284} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000285
Chris Lattner331ad772006-07-28 06:56:01 +0000286/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
287/// the two individual tokens to be lexed as a single token, return true (which
288/// causes a space to be printed between them). This allows the output of -E
289/// mode to be lexed to the same token stream as lexing the input directly
290/// would.
291///
292/// This code must conservatively return true if it doesn't want to be 100%
293/// accurate. This will cause the output to include extra space characters, but
294/// the resulting output won't have incorrect concatenations going on. Examples
295/// include "..", which we print with a space between, because we don't want to
296/// track enough to tell "x.." from "...".
297static bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok,
298 Preprocessor &PP) {
299 char Buffer[256];
300
301 // If we haven't emitted a token on this line yet, PrevTok isn't useful to
302 // look at and no concatenation could happen anyway.
303 if (!EmodeEmittedTokensOnThisLine)
304 return false;
305
306 // Basic algorithm: we look at the first character of the second token, and
307 // determine whether it, if appended to the first token, would form (or would
308 // contribute) to a larger token if concatenated.
309 char FirstChar;
310 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
311 // Avoid spelling identifiers, the most common form of token.
312 FirstChar = II->getName()[0];
313 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000314 const char *TokPtr = Buffer;
315 PP.getSpelling(Tok, TokPtr);
316 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000317 } else {
318 FirstChar = PP.getSpelling(Tok)[0];
319 }
320
321 tok::TokenKind PrevKind = PrevTok.getKind();
322 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
323 PrevKind = tok::identifier;
324
325 switch (PrevKind) {
326 default: return false;
327 case tok::identifier: // id+id or id+number or id+L"foo".
328 return isalnum(FirstChar) || FirstChar == '_';
329 case tok::numeric_constant:
330 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
331 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
332 case tok::period: // ..., .*, .1234
333 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
334 case tok::amp: // &&, &=
335 return FirstChar == '&' || FirstChar == '=';
336 case tok::plus: // ++, +=
337 return FirstChar == '+' || FirstChar == '=';
338 case tok::minus: // --, ->, -=, ->*
339 return FirstChar == '-' || FirstChar == '>' || FirstChar == '=';
340 case tok::slash: // /=, /*, //
341 return FirstChar == '=' || FirstChar == '*' || FirstChar == '/';
342 case tok::less: // <<, <<=, <=, <?=, <?, <:, <%
343 return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' ||
344 FirstChar == ':' || FirstChar == '%';
345 case tok::greater: // >>, >=, >>=, >?=, >?, ->*
346 return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' ||
347 FirstChar == '*';
348 case tok::pipe: // ||, |=
349 return FirstChar == '|' || FirstChar == '=';
350 case tok::percent: // %=, %>, %:
351 return FirstChar == '=' || FirstChar == '>' || FirstChar == ':';
352 case tok::colon: // ::, :>
353 return FirstChar == ':' || FirstChar == '>';
354 case tok::hash: // ##, #@, %:%:
355 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000356 case tok::arrow: // ->*
357 return FirstChar == '*';
358
359 case tok::star: // *=
360 case tok::exclaim: // !=
361 case tok::lessless: // <<=
362 case tok::greaterequal: // >>=
363 case tok::caret: // ^=
364 case tok::equal: // ==
Chris Lattner331ad772006-07-28 06:56:01 +0000365 // Cases that concatenate only if the next char is =.
366 return FirstChar == '=';
367 }
368}
369
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000370/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000371///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000372void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
373 LangOptions &Options) {
Chris Lattner457fc152006-07-29 06:30:25 +0000374 if (EnableCommentOutput) // -C specified?
375 Options.KeepComments = 1;
376 if (EnableMacroCommentOutput) // -CC specified?
377 Options.KeepComments = Options.KeepMacroComments = 1;
378
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000379 InitOutputBuffer();
380
Chris Lattner331ad772006-07-28 06:56:01 +0000381 LexerToken Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000382 char Buffer[256];
383 EModeCurLine = 0;
384 EModeCurFilename = "\"<uninit>\"";
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000385 PP.setPPCallbacks(new PrintPPOutputPPCallbacks());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000386 EModePP = &PP;
387 EmodeEmittedTokensOnThisLine = false;
388
389 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
390 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000391
392 // After we have configured the preprocessor, enter the main file.
393
394 // Start parsing the specified input file.
395 PP.EnterSourceFile(MainFileID, 0, true);
396
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000397 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000398 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000399 PP.Lex(Tok);
400
Chris Lattner67c38482006-07-04 23:24:26 +0000401 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000402 if (Tok.isAtStartOfLine()) {
403 HandleFirstTokOnLine(Tok, PP);
Chris Lattner331ad772006-07-28 06:56:01 +0000404 } else if (Tok.hasLeadingSpace() ||
405 // Don't print "-" next to "-", it would form "--".
406 AvoidConcat(PrevTok, Tok, PP)) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000407 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000408 }
409
410 if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000411 const char *TokPtr = Buffer;
412 unsigned Len = PP.getSpelling(Tok, TokPtr);
413 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000414 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000415 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000416 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000417 }
418 EmodeEmittedTokensOnThisLine = true;
419 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000420 OutputChar('\n');
421
422 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000423}
424