blob: e5f449185afa99decad23062c68cc1e6b00f8620 [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
151/// MoveToLine - Move the output to the source line specified by the location
152/// object. We can do this by emitting some number of \n's, or be emitting a
153/// #line directive.
154void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
155 if (DisableLineMarkers) {
156 if (EmittedTokensOnThisLine) {
157 OutputChar('\n');
158 EmittedTokensOnThisLine = false;
159 }
160 return;
161 }
162
Chris Lattner9dc1f532007-07-20 16:37:10 +0000163 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164
165 // If this line is "close enough" to the original line, just print newlines,
166 // otherwise print a #line directive.
167 if (LineNo-CurLine < 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000168 if (LineNo-CurLine == 1)
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 OutputChar('\n');
Chris Lattner822f9402007-07-23 05:14:05 +0000170 else {
171 const char *NewLines = "\n\n\n\n\n\n\n\n";
172 OutputString(NewLines, LineNo-CurLine);
173 CurLine = LineNo;
174 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 } else {
176 if (EmittedTokensOnThisLine) {
177 OutputChar('\n');
178 EmittedTokensOnThisLine = false;
179 }
180
181 CurLine = LineNo;
182
183 OutputChar('#');
184 OutputChar(' ');
185 std::string Num = llvm::utostr_32(LineNo);
186 OutputString(&Num[0], Num.size());
187 OutputChar(' ');
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000188 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000190 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000191
192 if (FileType == DirectoryLookup::SystemHeaderDir)
193 OutputString(" 3", 2);
194 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
195 OutputString(" 3 4", 4);
196 OutputChar('\n');
197 }
198}
199
200
201/// FileChanged - Whenever the preprocessor enters or exits a #include file
202/// it invokes this handler. Update our conception of the current source
203/// position.
204void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
205 FileChangeReason Reason,
206 DirectoryLookup::DirType FileType) {
207 if (DisableLineMarkers) return;
208
209 // Unless we are exiting a #include, make sure to skip ahead to the line the
210 // #include directive was at.
211 SourceManager &SourceMgr = PP.getSourceManager();
212 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000213 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
215 MoveToLine(Loc);
216
217 // TODO GCC emits the # directive for this directive on the line AFTER the
218 // directive and emits a bunch of spaces that aren't needed. Emulate this
219 // strange behavior.
220 }
221
Chris Lattner9dc1f532007-07-20 16:37:10 +0000222 Loc = SourceMgr.getLogicalLoc(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000224 CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 FileType = FileType;
226
227 if (EmittedTokensOnThisLine) {
228 OutputChar('\n');
229 EmittedTokensOnThisLine = false;
230 }
231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 OutputChar('#');
233 OutputChar(' ');
234 std::string Num = llvm::utostr_32(CurLine);
235 OutputString(&Num[0], Num.size());
236 OutputChar(' ');
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000237 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner0cbc4b52007-07-22 06:38:50 +0000239 OutputChar('"');
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
241 switch (Reason) {
242 case PPCallbacks::EnterFile:
243 OutputString(" 1", 2);
244 break;
245 case PPCallbacks::ExitFile:
246 OutputString(" 2", 2);
247 break;
248 case PPCallbacks::SystemHeaderPragma: break;
249 case PPCallbacks::RenameFile: break;
250 }
251
252 if (FileType == DirectoryLookup::SystemHeaderDir)
253 OutputString(" 3", 2);
254 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
255 OutputString(" 3 4", 4);
256
257 OutputChar('\n');
258}
259
260/// HandleIdent - Handle #ident directives when read by the preprocessor.
261///
262void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
263 MoveToLine(Loc);
264
265 OutputString("#ident ", strlen("#ident "));
266 OutputString(&S[0], S.size());
267 EmittedTokensOnThisLine = true;
268}
269
270/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
271/// is called for the first token on each new line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000272void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 // Figure out what line we went to and insert the appropriate number of
274 // newline characters.
275 MoveToLine(Tok.getLocation());
276
277 // Print out space characters so that the first token on a line is
278 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000279 const SourceManager &SourceMgr = PP.getSourceManager();
280 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000281
282 // This hack prevents stuff like:
283 // #define HASH #
284 // HASH define foo bar
285 // From having the # character end up at column 1, which makes it so it
286 // is not handled as a #define next time through the preprocessor if in
287 // -fpreprocessed mode.
288 if (ColNo <= 1 && Tok.getKind() == tok::hash)
289 OutputChar(' ');
290
291 // Otherwise, indent the appropriate number of spaces.
292 for (; ColNo > 1; --ColNo)
293 OutputChar(' ');
294}
295
296namespace {
297struct UnknownPragmaHandler : public PragmaHandler {
298 const char *Prefix;
299 PrintPPOutputPPCallbacks *Callbacks;
300
301 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
302 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000303 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Figure out what line we went to and insert the appropriate number of
305 // newline characters.
306 Callbacks->MoveToLine(PragmaTok.getLocation());
307 OutputString(Prefix, strlen(Prefix));
308
309 // Read and print all of the pragma tokens.
310 while (PragmaTok.getKind() != tok::eom) {
311 if (PragmaTok.hasLeadingSpace())
312 OutputChar(' ');
313 std::string TokSpell = PP.getSpelling(PragmaTok);
314 OutputString(&TokSpell[0], TokSpell.size());
315 PP.LexUnexpandedToken(PragmaTok);
316 }
317 OutputChar('\n');
318 }
319};
320} // end anonymous namespace
321
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000322
323enum AvoidConcatInfo {
324 /// By default, a token never needs to avoid concatenation. Most tokens (e.g.
325 /// ',', ')', etc) don't cause a problem when concatenated.
326 aci_never_avoid_concat = 0,
327
328 /// aci_custom_firstchar - AvoidConcat contains custom code to handle this
329 /// token's requirements, and it needs to know the first character of the
330 /// token.
331 aci_custom_firstchar = 1,
332
333 /// aci_custom - AvoidConcat contains custom code to handle this token's
334 /// requirements, but it doesn't need to know the first character of the
335 /// token.
336 aci_custom = 2,
337
338 /// aci_avoid_equal - Many tokens cannot be safely followed by an '='
339 /// character. For example, "<<" turns into "<<=" when followed by an =.
340 aci_avoid_equal = 4
341};
342
343/// This array contains information for each token on what action to take when
344/// avoiding concatenation of tokens in the AvoidConcat method.
345static char TokenInfo[tok::NUM_TOKENS];
346
347/// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be
348/// marked by this function.
349static void InitAvoidConcatTokenInfo() {
350 // These tokens have custom code in AvoidConcat.
351 TokenInfo[tok::identifier ] |= aci_custom;
352 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
353 TokenInfo[tok::period ] |= aci_custom_firstchar;
354 TokenInfo[tok::amp ] |= aci_custom_firstchar;
355 TokenInfo[tok::plus ] |= aci_custom_firstchar;
356 TokenInfo[tok::minus ] |= aci_custom_firstchar;
357 TokenInfo[tok::slash ] |= aci_custom_firstchar;
358 TokenInfo[tok::less ] |= aci_custom_firstchar;
359 TokenInfo[tok::greater ] |= aci_custom_firstchar;
360 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
361 TokenInfo[tok::percent ] |= aci_custom_firstchar;
362 TokenInfo[tok::colon ] |= aci_custom_firstchar;
363 TokenInfo[tok::hash ] |= aci_custom_firstchar;
364 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
365
366 // These tokens change behavior if followed by an '='.
367 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
368 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
369 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
370 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
371 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
372 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
373 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
374 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
375 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
376 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
377 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
378 TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>=
379 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
380 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
381}
382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
384/// the two individual tokens to be lexed as a single token, return true (which
385/// causes a space to be printed between them). This allows the output of -E
386/// mode to be lexed to the same token stream as lexing the input directly
387/// would.
388///
389/// This code must conservatively return true if it doesn't want to be 100%
390/// accurate. This will cause the output to include extra space characters, but
391/// the resulting output won't have incorrect concatenations going on. Examples
392/// include "..", which we print with a space between, because we don't want to
393/// track enough to tell "x.." from "...".
Chris Lattnerd2177732007-07-20 16:59:19 +0000394bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
395 const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000396 char Buffer[256];
397
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000398 tok::TokenKind PrevKind = PrevTok.getKind();
399 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
400 PrevKind = tok::identifier;
401
402 // Look up information on when we should avoid concatenation with prevtok.
403 unsigned ConcatInfo = TokenInfo[PrevKind];
404
405 // If prevtok never causes a problem for anything after it, return quickly.
406 if (ConcatInfo == 0) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000407
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000408 if (ConcatInfo & aci_avoid_equal) {
409 // If the next token is '=' or '==', avoid concatenation.
410 if (Tok.getKind() == tok::equal ||
411 Tok.getKind() == tok::equalequal)
412 return true;
413 ConcatInfo &= ~ConcatInfo;
414 }
415
416 if (ConcatInfo == 0) return false;
417
418
419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 // Basic algorithm: we look at the first character of the second token, and
421 // determine whether it, if appended to the first token, would form (or would
422 // contribute) to a larger token if concatenated.
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000423 char FirstChar = 0;
424 if (ConcatInfo & aci_custom) {
425 // If the token does not need to know the first character, don't get it.
426 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000427 // Avoid spelling identifiers, the most common form of token.
428 FirstChar = II->getName()[0];
Chris Lattnerb19f5e82007-07-23 05:18:42 +0000429 } else if (!Tok.needsCleaning()) {
430 SourceManager &SrcMgr = PP.getSourceManager();
431 FirstChar =
432 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 } else if (Tok.getLength() < 256) {
434 const char *TokPtr = Buffer;
435 PP.getSpelling(Tok, TokPtr);
436 FirstChar = TokPtr[0];
437 } else {
438 FirstChar = PP.getSpelling(Tok)[0];
439 }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 switch (PrevKind) {
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000442 default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 case tok::identifier: // id+id or id+number or id+L"foo".
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000444 if (Tok.getKind() == tok::numeric_constant || Tok.getIdentifierInfo() ||
445 Tok.getKind() == tok::wide_string_literal /* ||
446 Tok.getKind() == tok::wide_char_literal*/)
447 return true;
448 if (Tok.getKind() != tok::char_constant)
449 return false;
450
451 // FIXME: need a wide_char_constant!
452 if (!Tok.needsCleaning()) {
453 SourceManager &SrcMgr = PP.getSourceManager();
454 return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
455 == 'L';
456 } else if (Tok.getLength() < 256) {
457 const char *TokPtr = Buffer;
458 PP.getSpelling(Tok, TokPtr);
459 return TokPtr[0] == 'L';
460 } else {
461 return PP.getSpelling(Tok)[0] == 'L';
462 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 case tok::numeric_constant:
464 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
465 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
466 case tok::period: // ..., .*, .1234
467 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000468 case tok::amp: // &&
469 return FirstChar == '&';
470 case tok::plus: // ++
471 return FirstChar == '+';
472 case tok::minus: // --, ->, ->*
473 return FirstChar == '-' || FirstChar == '>';
474 case tok::slash: //, /*, //
475 return FirstChar == '*' || FirstChar == '/';
476 case tok::less: // <<, <<=, <:, <%
477 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
478 case tok::greater: // >>, >>=
479 return FirstChar == '>';
480 case tok::pipe: // ||
481 return FirstChar == '|';
482 case tok::percent: // %>, %:
483 return FirstChar == '>' || FirstChar == ':';
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 case tok::colon: // ::, :>
485 return FirstChar == ':' || FirstChar == '>';
486 case tok::hash: // ##, #@, %:%:
487 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
488 case tok::arrow: // ->*
489 return FirstChar == '*';
Reid Spencer5f016e22007-07-11 17:01:13 +0000490 }
491}
492
493/// DoPrintPreprocessedInput - This implements -E mode.
494///
495void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
496 const LangOptions &Options) {
497 // Inform the preprocessor whether we want it to retain comments or not, due
498 // to -C or -CC.
499 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
500
501 InitOutputBuffer();
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000502 InitAvoidConcatTokenInfo();
Reid Spencer5f016e22007-07-11 17:01:13 +0000503
Chris Lattnerd2177732007-07-20 16:59:19 +0000504 Token Tok, PrevTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 char Buffer[256];
506 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
507 PP.setPPCallbacks(Callbacks);
508
509 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
510 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
511
512 // After we have configured the preprocessor, enter the main file.
513
514 // Start parsing the specified input file.
515 PP.EnterSourceFile(MainFileID, 0, true);
516
517 do {
518 PrevTok = Tok;
519 PP.Lex(Tok);
520
521 // If this token is at the start of a line, emit newlines if needed.
522 if (Tok.isAtStartOfLine()) {
523 Callbacks->HandleFirstTokOnLine(Tok);
524 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000525 // If we haven't emitted a token on this line yet, PrevTok isn't
526 // useful to look at and no concatenation could happen anyway.
527 (!Callbacks->hasEmittedTokensOnThisLine() &&
528 // Don't print "-" next to "-", it would form "--".
529 Callbacks->AvoidConcat(PrevTok, Tok))) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 OutputChar(' ');
531 }
532
Chris Lattner2933f412007-07-23 06:14:36 +0000533 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
534 const char *Str = II->getName();
535 unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength();
536 OutputString(Str, Len);
537 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 const char *TokPtr = Buffer;
539 unsigned Len = PP.getSpelling(Tok, TokPtr);
540 OutputString(TokPtr, Len);
541 } else {
542 std::string S = PP.getSpelling(Tok);
543 OutputString(&S[0], S.size());
544 }
545 Callbacks->SetEmittedTokensOnThisLine();
546 } while (Tok.getKind() != tok::eof);
547 OutputChar('\n');
548
549 CleanupOutputBuffer();
550}
551