blob: cb8bfec3c4542bc864edfebf55473cd62a2bdb33 [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 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 Lattner23b7eb62007-06-15 23:05:46 +0000100static llvm::cl::opt<bool>
101DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
102static llvm::cl::opt<bool>
103EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
104static llvm::cl::opt<bool>
105EnableMacroCommentOutput("CC",
106 llvm::cl::desc("Enable comment output in -E mode, "
Chris Lattner457fc152006-07-29 06:30:25 +0000107 "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;
Chris Lattner9b796242007-07-22 06:38:50 +0000119 CurFilename = "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000120 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
Chris Lattner146762e2007-07-20 16:59:19 +0000131 void HandleFirstTokOnLine(Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000132 void MoveToLine(SourceLocation Loc);
Chris Lattner146762e2007-07-20 16:59:19 +0000133 bool AvoidConcat(const Token &PrevTok, const Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000134};
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
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000149 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(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) {
Chris Lattner5f075822007-07-23 05:14:05 +0000154 if (LineNo-CurLine == 1)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000155 OutputChar('\n');
Chris Lattner5f075822007-07-23 05:14:05 +0000156 else {
157 const char *NewLines = "\n\n\n\n\n\n\n\n";
158 OutputString(NewLines, LineNo-CurLine);
159 CurLine = LineNo;
160 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000161 } else {
Chris Lattner87f267e2006-11-21 05:02:33 +0000162 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000163 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000164 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000165 }
166
Chris Lattner87f267e2006-11-21 05:02:33 +0000167 CurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000168
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000169 OutputChar('#');
170 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000171 std::string Num = llvm::utostr_32(LineNo);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000172 OutputString(&Num[0], Num.size());
173 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000174 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000175 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000176 OutputChar('"');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000177
Chris Lattner87f267e2006-11-21 05:02:33 +0000178 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000179 OutputString(" 3", 2);
Chris Lattner87f267e2006-11-21 05:02:33 +0000180 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000181 OutputString(" 3 4", 4);
182 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000183 }
184}
185
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000186
187/// FileChanged - Whenever the preprocessor enters or exits a #include file
188/// it invokes this handler. Update our conception of the current source
189/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000190void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
191 FileChangeReason Reason,
192 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000193 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000194
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000195 // Unless we are exiting a #include, make sure to skip ahead to the line the
196 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000197 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000198 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000199 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000200 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000201 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000202
203 // TODO GCC emits the # directive for this directive on the line AFTER the
204 // directive and emits a bunch of spaces that aren't needed. Emulate this
205 // strange behavior.
206 }
207
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000208 Loc = SourceMgr.getLogicalLoc(Loc);
Chris Lattner87f267e2006-11-21 05:02:33 +0000209 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner9b796242007-07-22 06:38:50 +0000210 CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
Chris Lattner87f267e2006-11-21 05:02:33 +0000211 FileType = FileType;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000212
Chris Lattner87f267e2006-11-21 05:02:33 +0000213 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000214 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000215 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000216 }
217
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000218 OutputChar('#');
219 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000220 std::string Num = llvm::utostr_32(CurLine);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000221 OutputString(&Num[0], Num.size());
222 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000223 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000224 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000225 OutputChar('"');
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000226
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000227 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000228 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000229 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000230 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000231 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000232 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000233 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000234 case PPCallbacks::SystemHeaderPragma: break;
235 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000236 }
237
238 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000239 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000240 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000241 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000242
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000243 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000244}
245
Chris Lattner728b4dc2006-07-04 21:28:37 +0000246/// HandleIdent - Handle #ident directives when read by the preprocessor.
247///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000248void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000249 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000250
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000251 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000252 OutputString(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000253 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000254}
255
256/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
257/// is called for the first token on each new line.
Chris Lattner146762e2007-07-20 16:59:19 +0000258void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000259 // Figure out what line we went to and insert the appropriate number of
260 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000261 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000262
263 // Print out space characters so that the first token on a line is
264 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000265 const SourceManager &SourceMgr = PP.getSourceManager();
266 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000267
268 // This hack prevents stuff like:
269 // #define HASH #
270 // HASH define foo bar
271 // From having the # character end up at column 1, which makes it so it
272 // is not handled as a #define next time through the preprocessor if in
273 // -fpreprocessed mode.
274 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000275 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000276
277 // Otherwise, indent the appropriate number of spaces.
278 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000279 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000280}
281
Chris Lattner5de858c2006-07-04 19:04:44 +0000282namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000283struct UnknownPragmaHandler : public PragmaHandler {
284 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000285 PrintPPOutputPPCallbacks *Callbacks;
286
287 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
288 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000289 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000290 // Figure out what line we went to and insert the appropriate number of
291 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000292 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000293 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000294
295 // Read and print all of the pragma tokens.
296 while (PragmaTok.getKind() != tok::eom) {
297 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000298 OutputChar(' ');
299 std::string TokSpell = PP.getSpelling(PragmaTok);
300 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000301 PP.LexUnexpandedToken(PragmaTok);
302 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000303 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000304 }
305};
Chris Lattner5de858c2006-07-04 19:04:44 +0000306} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000307
Chris Lattner331ad772006-07-28 06:56:01 +0000308/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
309/// the two individual tokens to be lexed as a single token, return true (which
310/// causes a space to be printed between them). This allows the output of -E
311/// mode to be lexed to the same token stream as lexing the input directly
312/// would.
313///
314/// This code must conservatively return true if it doesn't want to be 100%
315/// accurate. This will cause the output to include extra space characters, but
316/// the resulting output won't have incorrect concatenations going on. Examples
317/// include "..", which we print with a space between, because we don't want to
318/// track enough to tell "x.." from "...".
Chris Lattner146762e2007-07-20 16:59:19 +0000319bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
320 const Token &Tok) {
Chris Lattner331ad772006-07-28 06:56:01 +0000321 char Buffer[256];
322
323 // If we haven't emitted a token on this line yet, PrevTok isn't useful to
324 // look at and no concatenation could happen anyway.
Chris Lattner87f267e2006-11-21 05:02:33 +0000325 if (!EmittedTokensOnThisLine)
Chris Lattner331ad772006-07-28 06:56:01 +0000326 return false;
327
328 // Basic algorithm: we look at the first character of the second token, and
329 // determine whether it, if appended to the first token, would form (or would
330 // contribute) to a larger token if concatenated.
331 char FirstChar;
332 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
333 // Avoid spelling identifiers, the most common form of token.
334 FirstChar = II->getName()[0];
Chris Lattnere4c566c2007-07-23 05:18:42 +0000335 } else if (!Tok.needsCleaning()) {
336 SourceManager &SrcMgr = PP.getSourceManager();
337 FirstChar =
338 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Chris Lattner331ad772006-07-28 06:56:01 +0000339 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000340 const char *TokPtr = Buffer;
341 PP.getSpelling(Tok, TokPtr);
342 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000343 } else {
344 FirstChar = PP.getSpelling(Tok)[0];
345 }
346
347 tok::TokenKind PrevKind = PrevTok.getKind();
348 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
349 PrevKind = tok::identifier;
350
351 switch (PrevKind) {
352 default: return false;
353 case tok::identifier: // id+id or id+number or id+L"foo".
354 return isalnum(FirstChar) || FirstChar == '_';
355 case tok::numeric_constant:
356 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
357 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
358 case tok::period: // ..., .*, .1234
359 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
360 case tok::amp: // &&, &=
361 return FirstChar == '&' || FirstChar == '=';
362 case tok::plus: // ++, +=
363 return FirstChar == '+' || FirstChar == '=';
364 case tok::minus: // --, ->, -=, ->*
365 return FirstChar == '-' || FirstChar == '>' || FirstChar == '=';
366 case tok::slash: // /=, /*, //
367 return FirstChar == '=' || FirstChar == '*' || FirstChar == '/';
368 case tok::less: // <<, <<=, <=, <?=, <?, <:, <%
369 return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' ||
370 FirstChar == ':' || FirstChar == '%';
Chris Lattner7dd7a1d2007-07-22 22:33:25 +0000371 case tok::greater: // >>, >=, >>=, >?=, >?
372 return FirstChar == '>' || FirstChar == '?' || FirstChar == '=';
Chris Lattner331ad772006-07-28 06:56:01 +0000373 case tok::pipe: // ||, |=
374 return FirstChar == '|' || FirstChar == '=';
375 case tok::percent: // %=, %>, %:
376 return FirstChar == '=' || FirstChar == '>' || FirstChar == ':';
377 case tok::colon: // ::, :>
378 return FirstChar == ':' || FirstChar == '>';
379 case tok::hash: // ##, #@, %:%:
380 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000381 case tok::arrow: // ->*
382 return FirstChar == '*';
383
384 case tok::star: // *=
385 case tok::exclaim: // !=
386 case tok::lessless: // <<=
387 case tok::greaterequal: // >>=
388 case tok::caret: // ^=
389 case tok::equal: // ==
Chris Lattner331ad772006-07-28 06:56:01 +0000390 // Cases that concatenate only if the next char is =.
391 return FirstChar == '=';
392 }
393}
394
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000395/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000396///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000397void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
Chris Lattner2ea9dd72006-11-21 06:18:11 +0000398 const LangOptions &Options) {
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000399 // Inform the preprocessor whether we want it to retain comments or not, due
400 // to -C or -CC.
401 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Chris Lattner457fc152006-07-29 06:30:25 +0000402
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000403 InitOutputBuffer();
404
Chris Lattner146762e2007-07-20 16:59:19 +0000405 Token Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000406 char Buffer[256];
Chris Lattner87f267e2006-11-21 05:02:33 +0000407 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
408 PP.setPPCallbacks(Callbacks);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000409
Chris Lattner87f267e2006-11-21 05:02:33 +0000410 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
411 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000412
413 // After we have configured the preprocessor, enter the main file.
414
415 // Start parsing the specified input file.
416 PP.EnterSourceFile(MainFileID, 0, true);
417
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000418 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000419 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000420 PP.Lex(Tok);
421
Chris Lattner67c38482006-07-04 23:24:26 +0000422 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000423 if (Tok.isAtStartOfLine()) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000424 Callbacks->HandleFirstTokOnLine(Tok);
Chris Lattner331ad772006-07-28 06:56:01 +0000425 } else if (Tok.hasLeadingSpace() ||
426 // Don't print "-" next to "-", it would form "--".
Chris Lattner87f267e2006-11-21 05:02:33 +0000427 Callbacks->AvoidConcat(PrevTok, Tok)) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000428 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000429 }
430
431 if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000432 const char *TokPtr = Buffer;
433 unsigned Len = PP.getSpelling(Tok, TokPtr);
434 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000435 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000436 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000437 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000438 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000439 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000440 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000441 OutputChar('\n');
442
443 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000444}
445