blob: e5f449185afa99decad23062c68cc1e6b00f8620 [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();
Chris Lattner93c4ea72007-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 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000104 OutBufCur += Size;
105#endif
106}
107
108
109//===----------------------------------------------------------------------===//
110// Preprocessed token printer
111//===----------------------------------------------------------------------===//
112
Chris Lattner23b7eb62007-06-15 23:05:46 +0000113static 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, "
Chris Lattner457fc152006-07-29 06:30:25 +0000120 "even from macro expansions"));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000121
Chris Lattner87f267e2006-11-21 05:02:33 +0000122namespace {
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 Lattner9b796242007-07-22 06:38:50 +0000132 CurFilename = "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000133 EmittedTokensOnThisLine = false;
134 FileType = DirectoryLookup::NormalHeaderDir;
135 }
136
137 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000138 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Chris Lattner87f267e2006-11-21 05:02:33 +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 Lattner146762e2007-07-20 16:59:19 +0000145 void HandleFirstTokOnLine(Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000146 void MoveToLine(SourceLocation Loc);
Chris Lattner146762e2007-07-20 16:59:19 +0000147 bool AvoidConcat(const Token &PrevTok, const Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000148};
149}
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000150
Chris Lattner728b4dc2006-07-04 21:28:37 +0000151/// 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.
Chris Lattner87f267e2006-11-21 05:02:33 +0000154void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000155 if (DisableLineMarkers) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000156 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000157 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000158 EmittedTokensOnThisLine = false;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000159 }
160 return;
161 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000162
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000163 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
Chris Lattner3338ba82006-07-04 21:19:39 +0000164
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000165 // If this line is "close enough" to the original line, just print newlines,
166 // otherwise print a #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000167 if (LineNo-CurLine < 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000168 if (LineNo-CurLine == 1)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000169 OutputChar('\n');
Chris Lattner5f075822007-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 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000175 } else {
Chris Lattner87f267e2006-11-21 05:02:33 +0000176 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000177 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000178 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000179 }
180
Chris Lattner87f267e2006-11-21 05:02:33 +0000181 CurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000182
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000183 OutputChar('#');
184 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000185 std::string Num = llvm::utostr_32(LineNo);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000186 OutputString(&Num[0], Num.size());
187 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000188 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000189 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000190 OutputChar('"');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000191
Chris Lattner87f267e2006-11-21 05:02:33 +0000192 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000193 OutputString(" 3", 2);
Chris Lattner87f267e2006-11-21 05:02:33 +0000194 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000195 OutputString(" 3 4", 4);
196 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000197 }
198}
199
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000200
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.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000204void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
205 FileChangeReason Reason,
206 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000207 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000208
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000209 // Unless we are exiting a #include, make sure to skip ahead to the line the
210 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000211 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000212 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000213 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000214 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000215 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000216
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 Lattnerdc5c0552007-07-20 16:37:10 +0000222 Loc = SourceMgr.getLogicalLoc(Loc);
Chris Lattner87f267e2006-11-21 05:02:33 +0000223 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner9b796242007-07-22 06:38:50 +0000224 CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
Chris Lattner87f267e2006-11-21 05:02:33 +0000225 FileType = FileType;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000226
Chris Lattner87f267e2006-11-21 05:02:33 +0000227 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000228 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000229 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000230 }
231
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000232 OutputChar('#');
233 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000234 std::string Num = llvm::utostr_32(CurLine);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000235 OutputString(&Num[0], Num.size());
236 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000237 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000238 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000239 OutputChar('"');
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000240
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000241 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000242 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000243 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000244 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000245 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000246 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000247 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000248 case PPCallbacks::SystemHeaderPragma: break;
249 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000250 }
251
252 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000253 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000254 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000255 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000256
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000257 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000258}
259
Chris Lattner728b4dc2006-07-04 21:28:37 +0000260/// HandleIdent - Handle #ident directives when read by the preprocessor.
261///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000262void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000263 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000264
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000265 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000266 OutputString(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000267 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000268}
269
270/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
271/// is called for the first token on each new line.
Chris Lattner146762e2007-07-20 16:59:19 +0000272void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000273 // Figure out what line we went to and insert the appropriate number of
274 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000275 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000276
277 // Print out space characters so that the first token on a line is
278 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000279 const SourceManager &SourceMgr = PP.getSourceManager();
280 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +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)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000289 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000290
291 // Otherwise, indent the appropriate number of spaces.
292 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000293 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000294}
295
Chris Lattner5de858c2006-07-04 19:04:44 +0000296namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000297struct UnknownPragmaHandler : public PragmaHandler {
298 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000299 PrintPPOutputPPCallbacks *Callbacks;
300
301 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
302 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000303 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000304 // Figure out what line we went to and insert the appropriate number of
305 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000306 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000307 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000308
309 // Read and print all of the pragma tokens.
310 while (PragmaTok.getKind() != tok::eom) {
311 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000312 OutputChar(' ');
313 std::string TokSpell = PP.getSpelling(PragmaTok);
314 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000315 PP.LexUnexpandedToken(PragmaTok);
316 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000317 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000318 }
319};
Chris Lattner5de858c2006-07-04 19:04:44 +0000320} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000321
Chris Lattner4418ce12007-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
Chris Lattner331ad772006-07-28 06:56:01 +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 Lattner146762e2007-07-20 16:59:19 +0000394bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
395 const Token &Tok) {
Chris Lattner331ad772006-07-28 06:56:01 +0000396 char Buffer[256];
397
Chris Lattner4418ce12007-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;
Chris Lattner331ad772006-07-28 06:56:01 +0000407
Chris Lattner4418ce12007-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
Chris Lattner331ad772006-07-28 06:56:01 +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 Lattner4418ce12007-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()) {
Chris Lattner331ad772006-07-28 06:56:01 +0000427 // Avoid spelling identifiers, the most common form of token.
428 FirstChar = II->getName()[0];
Chris Lattnere4c566c2007-07-23 05:18:42 +0000429 } else if (!Tok.needsCleaning()) {
430 SourceManager &SrcMgr = PP.getSourceManager();
431 FirstChar =
432 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Chris Lattner331ad772006-07-28 06:56:01 +0000433 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000434 const char *TokPtr = Buffer;
435 PP.getSpelling(Tok, TokPtr);
436 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000437 } else {
438 FirstChar = PP.getSpelling(Tok)[0];
439 }
Chris Lattner4418ce12007-07-23 06:09:34 +0000440
Chris Lattner331ad772006-07-28 06:56:01 +0000441 switch (PrevKind) {
Chris Lattner4418ce12007-07-23 06:09:34 +0000442 default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
Chris Lattner331ad772006-07-28 06:56:01 +0000443 case tok::identifier: // id+id or id+number or id+L"foo".
Chris Lattner4418ce12007-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 }
Chris Lattner331ad772006-07-28 06:56:01 +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 Lattner4418ce12007-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 == ':';
Chris Lattner331ad772006-07-28 06:56:01 +0000484 case tok::colon: // ::, :>
485 return FirstChar == ':' || FirstChar == '>';
486 case tok::hash: // ##, #@, %:%:
487 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000488 case tok::arrow: // ->*
489 return FirstChar == '*';
Chris Lattner331ad772006-07-28 06:56:01 +0000490 }
491}
492
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000493/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000494///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000495void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
Chris Lattner2ea9dd72006-11-21 06:18:11 +0000496 const LangOptions &Options) {
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000497 // Inform the preprocessor whether we want it to retain comments or not, due
498 // to -C or -CC.
499 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Chris Lattner457fc152006-07-29 06:30:25 +0000500
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000501 InitOutputBuffer();
Chris Lattner4418ce12007-07-23 06:09:34 +0000502 InitAvoidConcatTokenInfo();
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000503
Chris Lattner146762e2007-07-20 16:59:19 +0000504 Token Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000505 char Buffer[256];
Chris Lattner87f267e2006-11-21 05:02:33 +0000506 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
507 PP.setPPCallbacks(Callbacks);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000508
Chris Lattner87f267e2006-11-21 05:02:33 +0000509 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
510 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000511
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
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000517 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000518 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000519 PP.Lex(Tok);
520
Chris Lattner67c38482006-07-04 23:24:26 +0000521 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000522 if (Tok.isAtStartOfLine()) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000523 Callbacks->HandleFirstTokOnLine(Tok);
Chris Lattner331ad772006-07-28 06:56:01 +0000524 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-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))) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000530 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000531 }
532
Chris Lattner0af98232007-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) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000538 const char *TokPtr = Buffer;
539 unsigned Len = PP.getSpelling(Tok, TokPtr);
540 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000541 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000542 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000543 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000544 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000545 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000546 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000547 OutputChar('\n');
548
549 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000550}
551