blob: d5c5b9a08bb3447fae7447cc6430a662ba2e8739 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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/PPCallbacks.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Lex/Pragma.h"
19#include "clang/Basic/SourceManager.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/ADT/StringExtras.h"
22#include "llvm/Config/config.h"
23#include <cstdio>
24using namespace clang;
25
26//===----------------------------------------------------------------------===//
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();
Chris Lattnere225e372007-07-23 06:23:07 +000090
91 switch (Size) {
92 default:
93 memcpy(OutBufCur, Ptr, Size);
94 break;
95 case 3:
96 OutBufCur[2] = Ptr[2];
97 case 2:
98 OutBufCur[1] = Ptr[1];
99 case 1:
100 OutBufCur[0] = Ptr[0];
101 case 0:
102 break;
103 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 OutBufCur += Size;
105#endif
106}
107
108
109//===----------------------------------------------------------------------===//
110// Preprocessed token printer
111//===----------------------------------------------------------------------===//
112
113static llvm::cl::opt<bool>
114DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
115static llvm::cl::opt<bool>
116EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
117static llvm::cl::opt<bool>
118EnableMacroCommentOutput("CC",
119 llvm::cl::desc("Enable comment output in -E mode, "
120 "even from macro expansions"));
121
122namespace {
123class PrintPPOutputPPCallbacks : public PPCallbacks {
124 Preprocessor &PP;
125 unsigned CurLine;
126 std::string CurFilename;
127 bool EmittedTokensOnThisLine;
128 DirectoryLookup::DirType FileType;
129public:
130 PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) {
131 CurLine = 0;
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000132 CurFilename = "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 EmittedTokensOnThisLine = false;
134 FileType = DirectoryLookup::NormalHeaderDir;
135 }
136
137 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000138 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000139
140 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
141 DirectoryLookup::DirType FileType);
142 virtual void Ident(SourceLocation Loc, const std::string &str);
143
144
Chris Lattnerd2177732007-07-20 16:59:19 +0000145 void HandleFirstTokOnLine(Token &Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 void MoveToLine(SourceLocation Loc);
Chris Lattnerd2177732007-07-20 16:59:19 +0000147 bool AvoidConcat(const Token &PrevTok, const Token &Tok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148};
149}
150
Chris Lattnerf0637212007-07-23 06:31:11 +0000151/// UToStr - Do itoa on the specified number, in-place in the specified buffer.
152/// endptr points to the end of the buffer.
153static char *UToStr(unsigned N, char *EndPtr) {
154 // Null terminate the buffer.
155 *--EndPtr = '\0';
156 if (N == 0) // Zero is a special case.
157 *--EndPtr = '0';
158 while (N) {
159 *--EndPtr = '0' + char(N % 10);
160 N /= 10;
161 }
162 return EndPtr;
163}
164
165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166/// MoveToLine - Move the output to the source line specified by the location
167/// object. We can do this by emitting some number of \n's, or be emitting a
168/// #line directive.
169void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
170 if (DisableLineMarkers) {
171 if (EmittedTokensOnThisLine) {
172 OutputChar('\n');
173 EmittedTokensOnThisLine = false;
174 }
175 return;
176 }
177
Chris Lattner9dc1f532007-07-20 16:37:10 +0000178 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000179
180 // If this line is "close enough" to the original line, just print newlines,
181 // otherwise print a #line directive.
182 if (LineNo-CurLine < 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000183 if (LineNo-CurLine == 1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 OutputChar('\n');
Chris Lattner822f9402007-07-23 05:14:05 +0000185 else {
186 const char *NewLines = "\n\n\n\n\n\n\n\n";
187 OutputString(NewLines, LineNo-CurLine);
188 CurLine = LineNo;
189 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 } else {
191 if (EmittedTokensOnThisLine) {
192 OutputChar('\n');
193 EmittedTokensOnThisLine = false;
194 }
195
196 CurLine = LineNo;
197
198 OutputChar('#');
199 OutputChar(' ');
Chris Lattnerf0637212007-07-23 06:31:11 +0000200 char NumberBuffer[20];
201 const char *NumStr = UToStr(LineNo, NumberBuffer+20);
202 OutputString(NumStr, (NumberBuffer+20)-NumStr-1);
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 OutputChar(' ');
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000204 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000206 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000207
208 if (FileType == DirectoryLookup::SystemHeaderDir)
209 OutputString(" 3", 2);
210 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
211 OutputString(" 3 4", 4);
212 OutputChar('\n');
213 }
214}
215
216
217/// FileChanged - Whenever the preprocessor enters or exits a #include file
218/// it invokes this handler. Update our conception of the current source
219/// position.
220void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
221 FileChangeReason Reason,
222 DirectoryLookup::DirType FileType) {
223 if (DisableLineMarkers) return;
224
225 // Unless we are exiting a #include, make sure to skip ahead to the line the
226 // #include directive was at.
227 SourceManager &SourceMgr = PP.getSourceManager();
228 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000229 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
231 MoveToLine(Loc);
232
233 // TODO GCC emits the # directive for this directive on the line AFTER the
234 // directive and emits a bunch of spaces that aren't needed. Emulate this
235 // strange behavior.
236 }
237
Chris Lattner9dc1f532007-07-20 16:37:10 +0000238 Loc = SourceMgr.getLogicalLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000240 CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 FileType = FileType;
242
243 if (EmittedTokensOnThisLine) {
244 OutputChar('\n');
245 EmittedTokensOnThisLine = false;
246 }
247
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 OutputChar('#');
249 OutputChar(' ');
250 std::string Num = llvm::utostr_32(CurLine);
251 OutputString(&Num[0], Num.size());
252 OutputChar(' ');
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000253 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000255 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000256
257 switch (Reason) {
258 case PPCallbacks::EnterFile:
259 OutputString(" 1", 2);
260 break;
261 case PPCallbacks::ExitFile:
262 OutputString(" 2", 2);
263 break;
264 case PPCallbacks::SystemHeaderPragma: break;
265 case PPCallbacks::RenameFile: break;
266 }
267
268 if (FileType == DirectoryLookup::SystemHeaderDir)
269 OutputString(" 3", 2);
270 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
271 OutputString(" 3 4", 4);
272
273 OutputChar('\n');
274}
275
276/// HandleIdent - Handle #ident directives when read by the preprocessor.
277///
278void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
279 MoveToLine(Loc);
280
281 OutputString("#ident ", strlen("#ident "));
282 OutputString(&S[0], S.size());
283 EmittedTokensOnThisLine = true;
284}
285
286/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
287/// is called for the first token on each new line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000288void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // Figure out what line we went to and insert the appropriate number of
290 // newline characters.
291 MoveToLine(Tok.getLocation());
292
293 // Print out space characters so that the first token on a line is
294 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000295 const SourceManager &SourceMgr = PP.getSourceManager();
296 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000297
298 // This hack prevents stuff like:
299 // #define HASH #
300 // HASH define foo bar
301 // From having the # character end up at column 1, which makes it so it
302 // is not handled as a #define next time through the preprocessor if in
303 // -fpreprocessed mode.
304 if (ColNo <= 1 && Tok.getKind() == tok::hash)
305 OutputChar(' ');
306
307 // Otherwise, indent the appropriate number of spaces.
308 for (; ColNo > 1; --ColNo)
309 OutputChar(' ');
310}
311
312namespace {
313struct UnknownPragmaHandler : public PragmaHandler {
314 const char *Prefix;
315 PrintPPOutputPPCallbacks *Callbacks;
316
317 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
318 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000319 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000320 // Figure out what line we went to and insert the appropriate number of
321 // newline characters.
322 Callbacks->MoveToLine(PragmaTok.getLocation());
323 OutputString(Prefix, strlen(Prefix));
324
325 // Read and print all of the pragma tokens.
326 while (PragmaTok.getKind() != tok::eom) {
327 if (PragmaTok.hasLeadingSpace())
328 OutputChar(' ');
329 std::string TokSpell = PP.getSpelling(PragmaTok);
330 OutputString(&TokSpell[0], TokSpell.size());
331 PP.LexUnexpandedToken(PragmaTok);
332 }
333 OutputChar('\n');
334 }
335};
336} // end anonymous namespace
337
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000338
339enum AvoidConcatInfo {
340 /// By default, a token never needs to avoid concatenation. Most tokens (e.g.
341 /// ',', ')', etc) don't cause a problem when concatenated.
342 aci_never_avoid_concat = 0,
343
344 /// aci_custom_firstchar - AvoidConcat contains custom code to handle this
345 /// token's requirements, and it needs to know the first character of the
346 /// token.
347 aci_custom_firstchar = 1,
348
349 /// aci_custom - AvoidConcat contains custom code to handle this token's
350 /// requirements, but it doesn't need to know the first character of the
351 /// token.
352 aci_custom = 2,
353
354 /// aci_avoid_equal - Many tokens cannot be safely followed by an '='
355 /// character. For example, "<<" turns into "<<=" when followed by an =.
356 aci_avoid_equal = 4
357};
358
359/// This array contains information for each token on what action to take when
360/// avoiding concatenation of tokens in the AvoidConcat method.
361static char TokenInfo[tok::NUM_TOKENS];
362
363/// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be
364/// marked by this function.
365static void InitAvoidConcatTokenInfo() {
366 // These tokens have custom code in AvoidConcat.
367 TokenInfo[tok::identifier ] |= aci_custom;
368 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
369 TokenInfo[tok::period ] |= aci_custom_firstchar;
370 TokenInfo[tok::amp ] |= aci_custom_firstchar;
371 TokenInfo[tok::plus ] |= aci_custom_firstchar;
372 TokenInfo[tok::minus ] |= aci_custom_firstchar;
373 TokenInfo[tok::slash ] |= aci_custom_firstchar;
374 TokenInfo[tok::less ] |= aci_custom_firstchar;
375 TokenInfo[tok::greater ] |= aci_custom_firstchar;
376 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
377 TokenInfo[tok::percent ] |= aci_custom_firstchar;
378 TokenInfo[tok::colon ] |= aci_custom_firstchar;
379 TokenInfo[tok::hash ] |= aci_custom_firstchar;
380 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
381
382 // These tokens change behavior if followed by an '='.
383 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
384 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
385 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
386 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
387 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
388 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
389 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
390 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
391 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
392 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
393 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
394 TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>=
395 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
396 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
397}
398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
400/// the two individual tokens to be lexed as a single token, return true (which
401/// causes a space to be printed between them). This allows the output of -E
402/// mode to be lexed to the same token stream as lexing the input directly
403/// would.
404///
405/// This code must conservatively return true if it doesn't want to be 100%
406/// accurate. This will cause the output to include extra space characters, but
407/// the resulting output won't have incorrect concatenations going on. Examples
408/// include "..", which we print with a space between, because we don't want to
409/// track enough to tell "x.." from "...".
Chris Lattnerd2177732007-07-20 16:59:19 +0000410bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
411 const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 char Buffer[256];
413
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000414 tok::TokenKind PrevKind = PrevTok.getKind();
415 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
416 PrevKind = tok::identifier;
417
418 // Look up information on when we should avoid concatenation with prevtok.
419 unsigned ConcatInfo = TokenInfo[PrevKind];
420
421 // If prevtok never causes a problem for anything after it, return quickly.
422 if (ConcatInfo == 0) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000423
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000424 if (ConcatInfo & aci_avoid_equal) {
425 // If the next token is '=' or '==', avoid concatenation.
426 if (Tok.getKind() == tok::equal ||
427 Tok.getKind() == tok::equalequal)
428 return true;
Chris Lattnerb638a302007-07-23 23:21:34 +0000429 ConcatInfo &= ~aci_avoid_equal;
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000430 }
431
432 if (ConcatInfo == 0) return false;
433
434
435
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 // Basic algorithm: we look at the first character of the second token, and
437 // determine whether it, if appended to the first token, would form (or would
438 // contribute) to a larger token if concatenated.
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000439 char FirstChar = 0;
440 if (ConcatInfo & aci_custom) {
441 // If the token does not need to know the first character, don't get it.
442 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // Avoid spelling identifiers, the most common form of token.
444 FirstChar = II->getName()[0];
Chris Lattnerb19f5e82007-07-23 05:18:42 +0000445 } else if (!Tok.needsCleaning()) {
446 SourceManager &SrcMgr = PP.getSourceManager();
447 FirstChar =
448 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 } else if (Tok.getLength() < 256) {
450 const char *TokPtr = Buffer;
451 PP.getSpelling(Tok, TokPtr);
452 FirstChar = TokPtr[0];
453 } else {
454 FirstChar = PP.getSpelling(Tok)[0];
455 }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 switch (PrevKind) {
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000458 default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 case tok::identifier: // id+id or id+number or id+L"foo".
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000460 if (Tok.getKind() == tok::numeric_constant || Tok.getIdentifierInfo() ||
461 Tok.getKind() == tok::wide_string_literal /* ||
462 Tok.getKind() == tok::wide_char_literal*/)
463 return true;
464 if (Tok.getKind() != tok::char_constant)
465 return false;
466
467 // FIXME: need a wide_char_constant!
468 if (!Tok.needsCleaning()) {
469 SourceManager &SrcMgr = PP.getSourceManager();
470 return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
471 == 'L';
472 } else if (Tok.getLength() < 256) {
473 const char *TokPtr = Buffer;
474 PP.getSpelling(Tok, TokPtr);
475 return TokPtr[0] == 'L';
476 } else {
477 return PP.getSpelling(Tok)[0] == 'L';
478 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 case tok::numeric_constant:
480 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
481 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
482 case tok::period: // ..., .*, .1234
483 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000484 case tok::amp: // &&
485 return FirstChar == '&';
486 case tok::plus: // ++
487 return FirstChar == '+';
488 case tok::minus: // --, ->, ->*
489 return FirstChar == '-' || FirstChar == '>';
490 case tok::slash: //, /*, //
491 return FirstChar == '*' || FirstChar == '/';
492 case tok::less: // <<, <<=, <:, <%
493 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
494 case tok::greater: // >>, >>=
495 return FirstChar == '>';
496 case tok::pipe: // ||
497 return FirstChar == '|';
498 case tok::percent: // %>, %:
499 return FirstChar == '>' || FirstChar == ':';
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 case tok::colon: // ::, :>
501 return FirstChar == ':' || FirstChar == '>';
502 case tok::hash: // ##, #@, %:%:
503 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
504 case tok::arrow: // ->*
505 return FirstChar == '*';
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 }
507}
508
509/// DoPrintPreprocessedInput - This implements -E mode.
510///
511void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
512 const LangOptions &Options) {
513 // Inform the preprocessor whether we want it to retain comments or not, due
514 // to -C or -CC.
515 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
516
517 InitOutputBuffer();
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000518 InitAvoidConcatTokenInfo();
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
Chris Lattnerd2177732007-07-20 16:59:19 +0000520 Token Tok, PrevTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 char Buffer[256];
522 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
523 PP.setPPCallbacks(Callbacks);
524
525 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
526 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
527
528 // After we have configured the preprocessor, enter the main file.
529
530 // Start parsing the specified input file.
531 PP.EnterSourceFile(MainFileID, 0, true);
532
533 do {
534 PrevTok = Tok;
535 PP.Lex(Tok);
536
537 // If this token is at the start of a line, emit newlines if needed.
538 if (Tok.isAtStartOfLine()) {
539 Callbacks->HandleFirstTokOnLine(Tok);
540 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000541 // If we haven't emitted a token on this line yet, PrevTok isn't
542 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000543 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000544 // Don't print "-" next to "-", it would form "--".
545 Callbacks->AvoidConcat(PrevTok, Tok))) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 OutputChar(' ');
547 }
548
Chris Lattner2933f412007-07-23 06:14:36 +0000549 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
550 const char *Str = II->getName();
551 unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength();
552 OutputString(Str, Len);
553 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000554 const char *TokPtr = Buffer;
555 unsigned Len = PP.getSpelling(Tok, TokPtr);
556 OutputString(TokPtr, Len);
557 } else {
558 std::string S = PP.getSpelling(Tok);
559 OutputString(&S[0], S.size());
560 }
561 Callbacks->SetEmittedTokensOnThisLine();
562 } while (Tok.getKind() != tok::eof);
563 OutputChar('\n');
564
565 CleanupOutputBuffer();
566}
567