blob: 57b069e4f989f487f93fa0af9493440c04951fc9 [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"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/Pragma.h"
18#include "clang/Basic/SourceManager.h"
19#include "llvm/Support/CommandLine.h"
Chris Lattnerf46be6c2006-07-04 22:19:33 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/Config/config.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000022#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000023using namespace llvm;
24using namespace clang;
25
Chris Lattnerf46be6c2006-07-04 22:19:33 +000026//===----------------------------------------------------------------------===//
27// Simple buffered I/O
28//===----------------------------------------------------------------------===//
29//
30// Empirically, iostream is over 30% slower than stdio for this workload, and
31// stdio itself isn't very well suited. The problem with stdio is use of
32// putchar_unlocked. We have many newline characters that need to be emitted,
33// but stdio needs to do extra checks to handle line buffering mode. These
34// extra checks make putchar_unlocked fall off its inlined code path, hitting
35// slow system code. In practice, using 'write' directly makes 'clang -E -P'
36// about 10% faster than using the stdio path on darwin.
37
38#ifdef HAVE_UNISTD_H
39#include <unistd.h>
40#else
41#define USE_STDIO 1
42#endif
43
44static char *OutBufStart = 0, *OutBufEnd, *OutBufCur;
45
46/// InitOutputBuffer - Initialize our output buffer.
47///
48static void InitOutputBuffer() {
49#ifndef USE_STDIO
50 OutBufStart = new char[64*1024];
51 OutBufEnd = OutBufStart+64*1024;
52 OutBufCur = OutBufStart;
53#endif
54}
55
56/// FlushBuffer - Write the accumulated bytes to the output stream.
57///
58static void FlushBuffer() {
59#ifndef USE_STDIO
60 write(STDOUT_FILENO, OutBufStart, OutBufCur-OutBufStart);
61 OutBufCur = OutBufStart;
62#endif
63}
64
65/// CleanupOutputBuffer - Finish up output.
66///
67static void CleanupOutputBuffer() {
68#ifndef USE_STDIO
69 FlushBuffer();
70 delete [] OutBufStart;
71#endif
72}
73
74static void OutputChar(char c) {
75#ifdef USE_STDIO
76 putchar_unlocked(c);
77#else
78 if (OutBufCur >= OutBufEnd)
79 FlushBuffer();
80 *OutBufCur++ = c;
81#endif
82}
83
84static void OutputString(const char *Ptr, unsigned Size) {
85#ifdef USE_STDIO
86 fwrite(Ptr, Size, 1, stdout);
87#else
88 if (OutBufCur+Size >= OutBufEnd)
89 FlushBuffer();
90 memcpy(OutBufCur, Ptr, Size);
91 OutBufCur += Size;
92#endif
93}
94
95
96//===----------------------------------------------------------------------===//
97// Preprocessed token printer
98//===----------------------------------------------------------------------===//
99
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000100static cl::opt<bool>
101DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
Chris Lattner457fc152006-07-29 06:30:25 +0000102static cl::opt<bool>
103EnableCommentOutput("C", cl::desc("Enable comment output in -E mode"));
104static cl::opt<bool>
105EnableMacroCommentOutput("CC", cl::desc("Enable comment output in -E mode, "
106 "even from macro expansions"));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000107
108static unsigned EModeCurLine;
109static std::string EModeCurFilename;
110static Preprocessor *EModePP;
111static bool EmodeEmittedTokensOnThisLine;
112static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
113
Chris Lattner728b4dc2006-07-04 21:28:37 +0000114/// MoveToLine - Move the output to the source line specified by the location
115/// object. We can do this by emitting some number of \n's, or be emitting a
116/// #line directive.
Chris Lattner3338ba82006-07-04 21:19:39 +0000117static void MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000118 if (DisableLineMarkers) {
119 if (EmodeEmittedTokensOnThisLine) {
120 OutputChar('\n');
121 EmodeEmittedTokensOnThisLine = false;
122 }
123 return;
124 }
Chris Lattner3338ba82006-07-04 21:19:39 +0000125
126 unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc);
127
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000128 // If this line is "close enough" to the original line, just print newlines,
129 // otherwise print a #line directive.
130 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000131 unsigned CurLine = EModeCurLine;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000132 for (; CurLine != LineNo; ++CurLine)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000133 OutputChar('\n');
Chris Lattnerdeb37012006-07-04 19:24:06 +0000134 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000135 } else {
136 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000137 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000138 EmodeEmittedTokensOnThisLine = false;
139 }
140
141 EModeCurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000142
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000143 OutputChar('#');
144 OutputChar(' ');
145 std::string Num = utostr_32(LineNo);
146 OutputString(&Num[0], Num.size());
147 OutputChar(' ');
148 OutputString(&EModeCurFilename[0], EModeCurFilename.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000149
150 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000151 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000152 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000153 OutputString(" 3 4", 4);
154 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000155 }
156}
157
158/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
159/// it invokes this handler. Update our conception of the current
160static void HandleFileChange(SourceLocation Loc,
161 Preprocessor::FileChangeReason Reason,
162 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000163 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000164
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000165 // Unless we are exiting a #include, make sure to skip ahead to the line the
166 // #include directive was at.
Chris Lattnerff3f5f42006-07-04 21:25:59 +0000167 SourceManager &SourceMgr = EModePP->getSourceManager();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000168 if (Reason == Preprocessor::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000169 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000170 } else if (Reason == Preprocessor::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000171 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000172
173 // TODO GCC emits the # directive for this directive on the line AFTER the
174 // directive and emits a bunch of spaces that aren't needed. Emulate this
175 // strange behavior.
176 }
177
178 EModeCurLine = SourceMgr.getLineNumber(Loc);
Chris Lattnerecc39e92006-07-15 05:23:31 +0000179 EModeCurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000180 EmodeFileType = FileType;
181
182 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000183 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000184 EmodeEmittedTokensOnThisLine = false;
185 }
186
187 if (DisableLineMarkers) return;
188
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000189 OutputChar('#');
190 OutputChar(' ');
191 std::string Num = utostr_32(EModeCurLine);
192 OutputString(&Num[0], Num.size());
193 OutputChar(' ');
194 OutputString(&EModeCurFilename[0], EModeCurFilename.size());
195
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000196 switch (Reason) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000197 case Preprocessor::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000198 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000199 break;
200 case Preprocessor::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000201 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000202 break;
203 case Preprocessor::SystemHeaderPragma: break;
204 case Preprocessor::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000205 }
206
207 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000208 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000209 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000210 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000211
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000212 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000213}
214
Chris Lattner728b4dc2006-07-04 21:28:37 +0000215/// HandleIdent - Handle #ident directives when read by the preprocessor.
216///
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000217static void HandleIdent(SourceLocation Loc, const std::string &Val) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000218 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000219
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000220 OutputString("#ident ", strlen("#ident "));
221 OutputString(&Val[0], Val.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000222 EmodeEmittedTokensOnThisLine = true;
223}
224
225/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
226/// is called for the first token on each new line.
227static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
228 // Figure out what line we went to and insert the appropriate number of
229 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000230 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000231
232 // Print out space characters so that the first token on a line is
233 // indented for easy reading.
234 unsigned ColNo =
235 PP.getSourceManager().getColumnNumber(Tok.getLocation());
236
237 // This hack prevents stuff like:
238 // #define HASH #
239 // HASH define foo bar
240 // From having the # character end up at column 1, which makes it so it
241 // is not handled as a #define next time through the preprocessor if in
242 // -fpreprocessed mode.
243 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000244 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000245
246 // Otherwise, indent the appropriate number of spaces.
247 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000248 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000249}
250
Chris Lattner5de858c2006-07-04 19:04:44 +0000251namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000252struct UnknownPragmaHandler : public PragmaHandler {
253 const char *Prefix;
254 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
255 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
256 // Figure out what line we went to and insert the appropriate number of
257 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000258 MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000259 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000260
261 // Read and print all of the pragma tokens.
262 while (PragmaTok.getKind() != tok::eom) {
263 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000264 OutputChar(' ');
265 std::string TokSpell = PP.getSpelling(PragmaTok);
266 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000267 PP.LexUnexpandedToken(PragmaTok);
268 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000269 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000270 }
271};
Chris Lattner5de858c2006-07-04 19:04:44 +0000272} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000273
Chris Lattner331ad772006-07-28 06:56:01 +0000274/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
275/// the two individual tokens to be lexed as a single token, return true (which
276/// causes a space to be printed between them). This allows the output of -E
277/// mode to be lexed to the same token stream as lexing the input directly
278/// would.
279///
280/// This code must conservatively return true if it doesn't want to be 100%
281/// accurate. This will cause the output to include extra space characters, but
282/// the resulting output won't have incorrect concatenations going on. Examples
283/// include "..", which we print with a space between, because we don't want to
284/// track enough to tell "x.." from "...".
285static bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok,
286 Preprocessor &PP) {
287 char Buffer[256];
288
289 // If we haven't emitted a token on this line yet, PrevTok isn't useful to
290 // look at and no concatenation could happen anyway.
291 if (!EmodeEmittedTokensOnThisLine)
292 return false;
293
294 // Basic algorithm: we look at the first character of the second token, and
295 // determine whether it, if appended to the first token, would form (or would
296 // contribute) to a larger token if concatenated.
297 char FirstChar;
298 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
299 // Avoid spelling identifiers, the most common form of token.
300 FirstChar = II->getName()[0];
301 } else if (Tok.getLength() < 256) {
302 const char *TokPtr = Buffer;
303 unsigned Len = PP.getSpelling(Tok, TokPtr);
304 FirstChar = TokPtr[0];
305 } else {
306 FirstChar = PP.getSpelling(Tok)[0];
307 }
308
309 tok::TokenKind PrevKind = PrevTok.getKind();
310 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
311 PrevKind = tok::identifier;
312
313 switch (PrevKind) {
314 default: return false;
315 case tok::identifier: // id+id or id+number or id+L"foo".
316 return isalnum(FirstChar) || FirstChar == '_';
317 case tok::numeric_constant:
318 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
319 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
320 case tok::period: // ..., .*, .1234
321 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
322 case tok::amp: // &&, &=
323 return FirstChar == '&' || FirstChar == '=';
324 case tok::plus: // ++, +=
325 return FirstChar == '+' || FirstChar == '=';
326 case tok::minus: // --, ->, -=, ->*
327 return FirstChar == '-' || FirstChar == '>' || FirstChar == '=';
328 case tok::slash: // /=, /*, //
329 return FirstChar == '=' || FirstChar == '*' || FirstChar == '/';
330 case tok::less: // <<, <<=, <=, <?=, <?, <:, <%
331 return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' ||
332 FirstChar == ':' || FirstChar == '%';
333 case tok::greater: // >>, >=, >>=, >?=, >?, ->*
334 return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' ||
335 FirstChar == '*';
336 case tok::pipe: // ||, |=
337 return FirstChar == '|' || FirstChar == '=';
338 case tok::percent: // %=, %>, %:
339 return FirstChar == '=' || FirstChar == '>' || FirstChar == ':';
340 case tok::colon: // ::, :>
341 return FirstChar == ':' || FirstChar == '>';
342 case tok::hash: // ##, #@, %:%:
343 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
344 case tok::question: // <?=, >?=, ??x -> trigraphs.
345 // Have to check for <?= in case <? is disabled.
346 return FirstChar == '?' || FirstChar == '=';
347 case tok::arrow: // ->*
348 return FirstChar == '*';
349
350 case tok::star: // *=
351 case tok::exclaim: // !=
352 case tok::lessless: // <<=
353 case tok::greaterequal: // >>=
354 case tok::caret: // ^=
355 case tok::equal: // ==
356 case tok::lessquestion: // <?=
357 case tok::greaterquestion: // >?=
358 // Cases that concatenate only if the next char is =.
359 return FirstChar == '=';
360 }
361}
362
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000363/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000364///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000365void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
366 LangOptions &Options) {
Chris Lattner457fc152006-07-29 06:30:25 +0000367 if (EnableCommentOutput) // -C specified?
368 Options.KeepComments = 1;
369 if (EnableMacroCommentOutput) // -CC specified?
370 Options.KeepComments = Options.KeepMacroComments = 1;
371
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000372 InitOutputBuffer();
373
Chris Lattner331ad772006-07-28 06:56:01 +0000374 LexerToken Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000375 char Buffer[256];
376 EModeCurLine = 0;
377 EModeCurFilename = "\"<uninit>\"";
378 PP.setFileChangeHandler(HandleFileChange);
379 PP.setIdentHandler(HandleIdent);
380 EModePP = &PP;
381 EmodeEmittedTokensOnThisLine = false;
382
383 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
384 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000385
386 // After we have configured the preprocessor, enter the main file.
387
388 // Start parsing the specified input file.
389 PP.EnterSourceFile(MainFileID, 0, true);
390
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000391 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000392 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000393 PP.Lex(Tok);
394
Chris Lattner67c38482006-07-04 23:24:26 +0000395 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000396 if (Tok.isAtStartOfLine()) {
397 HandleFirstTokOnLine(Tok, PP);
Chris Lattner331ad772006-07-28 06:56:01 +0000398 } else if (Tok.hasLeadingSpace() ||
399 // Don't print "-" next to "-", it would form "--".
400 AvoidConcat(PrevTok, Tok, PP)) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000401 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000402 }
403
404 if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000405 const char *TokPtr = Buffer;
406 unsigned Len = PP.getSpelling(Tok, TokPtr);
407 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000408 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000409 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000410 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000411 }
412 EmodeEmittedTokensOnThisLine = true;
413 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000414 OutputChar('\n');
415
416 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000417}
418