blob: bf5c3a2cac53d2799a36630b03406b32cc3d0d30 [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
Chris Lattner87f267e2006-11-21 05:02:33 +0000109namespace {
110class PrintPPOutputPPCallbacks : public PPCallbacks {
111 Preprocessor &PP;
112 unsigned CurLine;
113 std::string CurFilename;
114 bool EmittedTokensOnThisLine;
115 DirectoryLookup::DirType FileType;
116public:
117 PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) {
118 CurLine = 0;
119 CurFilename = "\"<uninit>\"";
120 EmittedTokensOnThisLine = false;
121 FileType = DirectoryLookup::NormalHeaderDir;
122 }
123
124 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
125
126 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
127 DirectoryLookup::DirType FileType);
128 virtual void Ident(SourceLocation Loc, const std::string &str);
129
130
131 void HandleFirstTokOnLine(LexerToken &Tok);
132 void MoveToLine(SourceLocation Loc);
133 bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok);
134};
135}
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000136
Chris Lattner728b4dc2006-07-04 21:28:37 +0000137/// MoveToLine - Move the output to the source line specified by the location
138/// object. We can do this by emitting some number of \n's, or be emitting a
139/// #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000140void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000141 if (DisableLineMarkers) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000142 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000143 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000144 EmittedTokensOnThisLine = false;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000145 }
146 return;
147 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000148
149 unsigned LineNo = PP.getSourceManager().getLineNumber(Loc);
Chris Lattner3338ba82006-07-04 21:19:39 +0000150
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000151 // If this line is "close enough" to the original line, just print newlines,
152 // otherwise print a #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000153 if (LineNo-CurLine < 8) {
154 unsigned Line = CurLine;
155 for (; Line != LineNo; ++Line)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000156 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000157 CurLine = Line;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000158 } else {
Chris Lattner87f267e2006-11-21 05:02:33 +0000159 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000160 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000161 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000162 }
163
Chris Lattner87f267e2006-11-21 05:02:33 +0000164 CurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000165
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000166 OutputChar('#');
167 OutputChar(' ');
168 std::string Num = utostr_32(LineNo);
169 OutputString(&Num[0], Num.size());
170 OutputChar(' ');
Chris Lattner87f267e2006-11-21 05:02:33 +0000171 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000172
Chris Lattner87f267e2006-11-21 05:02:33 +0000173 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000174 OutputString(" 3", 2);
Chris Lattner87f267e2006-11-21 05:02:33 +0000175 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000176 OutputString(" 3 4", 4);
177 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000178 }
179}
180
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000181
182/// FileChanged - Whenever the preprocessor enters or exits a #include file
183/// it invokes this handler. Update our conception of the current source
184/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000185void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
186 FileChangeReason Reason,
187 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000188 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000189
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000190 // Unless we are exiting a #include, make sure to skip ahead to the line the
191 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000192 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000193 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000194 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000195 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000196 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000197
198 // TODO GCC emits the # directive for this directive on the line AFTER the
199 // directive and emits a bunch of spaces that aren't needed. Emulate this
200 // strange behavior.
201 }
202
Chris Lattner87f267e2006-11-21 05:02:33 +0000203 CurLine = SourceMgr.getLineNumber(Loc);
204 CurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"';
205 FileType = FileType;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000206
Chris Lattner87f267e2006-11-21 05:02:33 +0000207 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000208 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000209 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000210 }
211
212 if (DisableLineMarkers) return;
213
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000214 OutputChar('#');
215 OutputChar(' ');
Chris Lattner87f267e2006-11-21 05:02:33 +0000216 std::string Num = utostr_32(CurLine);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000217 OutputString(&Num[0], Num.size());
218 OutputChar(' ');
Chris Lattner87f267e2006-11-21 05:02:33 +0000219 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000220
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000221 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000222 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000223 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000224 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000225 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000226 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000227 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000228 case PPCallbacks::SystemHeaderPragma: break;
229 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000230 }
231
232 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000233 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000234 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000235 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000236
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000237 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000238}
239
Chris Lattner728b4dc2006-07-04 21:28:37 +0000240/// HandleIdent - Handle #ident directives when read by the preprocessor.
241///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000242void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000243 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000244
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000245 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000246 OutputString(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000247 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000248}
249
250/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
251/// is called for the first token on each new line.
Chris Lattner87f267e2006-11-21 05:02:33 +0000252void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(LexerToken &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000253 // Figure out what line we went to and insert the appropriate number of
254 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000255 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000256
257 // Print out space characters so that the first token on a line is
258 // indented for easy reading.
259 unsigned ColNo =
260 PP.getSourceManager().getColumnNumber(Tok.getLocation());
261
262 // This hack prevents stuff like:
263 // #define HASH #
264 // HASH define foo bar
265 // From having the # character end up at column 1, which makes it so it
266 // is not handled as a #define next time through the preprocessor if in
267 // -fpreprocessed mode.
268 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000269 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000270
271 // Otherwise, indent the appropriate number of spaces.
272 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000273 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000274}
275
Chris Lattner5de858c2006-07-04 19:04:44 +0000276namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000277struct UnknownPragmaHandler : public PragmaHandler {
278 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000279 PrintPPOutputPPCallbacks *Callbacks;
280
281 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
282 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000283 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
284 // Figure out what line we went to and insert the appropriate number of
285 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000286 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000287 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000288
289 // Read and print all of the pragma tokens.
290 while (PragmaTok.getKind() != tok::eom) {
291 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000292 OutputChar(' ');
293 std::string TokSpell = PP.getSpelling(PragmaTok);
294 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000295 PP.LexUnexpandedToken(PragmaTok);
296 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000297 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000298 }
299};
Chris Lattner5de858c2006-07-04 19:04:44 +0000300} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000301
Chris Lattner331ad772006-07-28 06:56:01 +0000302/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
303/// the two individual tokens to be lexed as a single token, return true (which
304/// causes a space to be printed between them). This allows the output of -E
305/// mode to be lexed to the same token stream as lexing the input directly
306/// would.
307///
308/// This code must conservatively return true if it doesn't want to be 100%
309/// accurate. This will cause the output to include extra space characters, but
310/// the resulting output won't have incorrect concatenations going on. Examples
311/// include "..", which we print with a space between, because we don't want to
312/// track enough to tell "x.." from "...".
Chris Lattner87f267e2006-11-21 05:02:33 +0000313bool PrintPPOutputPPCallbacks::AvoidConcat(const LexerToken &PrevTok,
314 const LexerToken &Tok) {
Chris Lattner331ad772006-07-28 06:56:01 +0000315 char Buffer[256];
316
317 // If we haven't emitted a token on this line yet, PrevTok isn't useful to
318 // look at and no concatenation could happen anyway.
Chris Lattner87f267e2006-11-21 05:02:33 +0000319 if (!EmittedTokensOnThisLine)
Chris Lattner331ad772006-07-28 06:56:01 +0000320 return false;
321
322 // Basic algorithm: we look at the first character of the second token, and
323 // determine whether it, if appended to the first token, would form (or would
324 // contribute) to a larger token if concatenated.
325 char FirstChar;
326 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
327 // Avoid spelling identifiers, the most common form of token.
328 FirstChar = II->getName()[0];
329 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000330 const char *TokPtr = Buffer;
331 PP.getSpelling(Tok, TokPtr);
332 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000333 } else {
334 FirstChar = PP.getSpelling(Tok)[0];
335 }
336
337 tok::TokenKind PrevKind = PrevTok.getKind();
338 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
339 PrevKind = tok::identifier;
340
341 switch (PrevKind) {
342 default: return false;
343 case tok::identifier: // id+id or id+number or id+L"foo".
344 return isalnum(FirstChar) || FirstChar == '_';
345 case tok::numeric_constant:
346 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
347 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
348 case tok::period: // ..., .*, .1234
349 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
350 case tok::amp: // &&, &=
351 return FirstChar == '&' || FirstChar == '=';
352 case tok::plus: // ++, +=
353 return FirstChar == '+' || FirstChar == '=';
354 case tok::minus: // --, ->, -=, ->*
355 return FirstChar == '-' || FirstChar == '>' || FirstChar == '=';
356 case tok::slash: // /=, /*, //
357 return FirstChar == '=' || FirstChar == '*' || FirstChar == '/';
358 case tok::less: // <<, <<=, <=, <?=, <?, <:, <%
359 return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' ||
360 FirstChar == ':' || FirstChar == '%';
361 case tok::greater: // >>, >=, >>=, >?=, >?, ->*
362 return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' ||
363 FirstChar == '*';
364 case tok::pipe: // ||, |=
365 return FirstChar == '|' || FirstChar == '=';
366 case tok::percent: // %=, %>, %:
367 return FirstChar == '=' || FirstChar == '>' || FirstChar == ':';
368 case tok::colon: // ::, :>
369 return FirstChar == ':' || FirstChar == '>';
370 case tok::hash: // ##, #@, %:%:
371 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000372 case tok::arrow: // ->*
373 return FirstChar == '*';
374
375 case tok::star: // *=
376 case tok::exclaim: // !=
377 case tok::lessless: // <<=
378 case tok::greaterequal: // >>=
379 case tok::caret: // ^=
380 case tok::equal: // ==
Chris Lattner331ad772006-07-28 06:56:01 +0000381 // Cases that concatenate only if the next char is =.
382 return FirstChar == '=';
383 }
384}
385
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000386/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000387///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000388void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
Chris Lattner2ea9dd72006-11-21 06:18:11 +0000389 const LangOptions &Options) {
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000390 // Inform the preprocessor whether we want it to retain comments or not, due
391 // to -C or -CC.
392 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Chris Lattner457fc152006-07-29 06:30:25 +0000393
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000394 InitOutputBuffer();
395
Chris Lattner331ad772006-07-28 06:56:01 +0000396 LexerToken Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000397 char Buffer[256];
Chris Lattner87f267e2006-11-21 05:02:33 +0000398 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
399 PP.setPPCallbacks(Callbacks);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000400
Chris Lattner87f267e2006-11-21 05:02:33 +0000401 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
402 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000403
404 // After we have configured the preprocessor, enter the main file.
405
406 // Start parsing the specified input file.
407 PP.EnterSourceFile(MainFileID, 0, true);
408
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000409 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000410 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000411 PP.Lex(Tok);
412
Chris Lattner67c38482006-07-04 23:24:26 +0000413 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000414 if (Tok.isAtStartOfLine()) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000415 Callbacks->HandleFirstTokOnLine(Tok);
Chris Lattner331ad772006-07-28 06:56:01 +0000416 } else if (Tok.hasLeadingSpace() ||
417 // Don't print "-" next to "-", it would form "--".
Chris Lattner87f267e2006-11-21 05:02:33 +0000418 Callbacks->AvoidConcat(PrevTok, Tok)) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000419 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000420 }
421
422 if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000423 const char *TokPtr = Buffer;
424 unsigned Len = PP.getSpelling(Tok, TokPtr);
425 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000426 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000427 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000428 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000429 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000430 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000431 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000432 OutputChar('\n');
433
434 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000435}
436