blob: a7d55df34b4a10932b03e124d251e1fe9fd6de6e [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; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000125 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Chris Lattner87f267e2006-11-21 05:02:33 +0000126
127 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
128 DirectoryLookup::DirType FileType);
129 virtual void Ident(SourceLocation Loc, const std::string &str);
130
131
Chris Lattner146762e2007-07-20 16:59:19 +0000132 void HandleFirstTokOnLine(Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000133 void MoveToLine(SourceLocation Loc);
Chris Lattner146762e2007-07-20 16:59:19 +0000134 bool AvoidConcat(const Token &PrevTok, const Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000135};
136}
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000137
Chris Lattner728b4dc2006-07-04 21:28:37 +0000138/// MoveToLine - Move the output to the source line specified by the location
139/// object. We can do this by emitting some number of \n's, or be emitting a
140/// #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000141void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000142 if (DisableLineMarkers) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000143 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000144 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000145 EmittedTokensOnThisLine = false;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000146 }
147 return;
148 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000149
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000150 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
Chris Lattner3338ba82006-07-04 21:19:39 +0000151
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000152 // If this line is "close enough" to the original line, just print newlines,
153 // otherwise print a #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000154 if (LineNo-CurLine < 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000155 if (LineNo-CurLine == 1)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000156 OutputChar('\n');
Chris Lattner5f075822007-07-23 05:14:05 +0000157 else {
158 const char *NewLines = "\n\n\n\n\n\n\n\n";
159 OutputString(NewLines, LineNo-CurLine);
160 CurLine = LineNo;
161 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000162 } else {
Chris Lattner87f267e2006-11-21 05:02:33 +0000163 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000164 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000165 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000166 }
167
Chris Lattner87f267e2006-11-21 05:02:33 +0000168 CurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000169
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000170 OutputChar('#');
171 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000172 std::string Num = llvm::utostr_32(LineNo);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000173 OutputString(&Num[0], Num.size());
174 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000175 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000176 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000177 OutputChar('"');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000178
Chris Lattner87f267e2006-11-21 05:02:33 +0000179 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000180 OutputString(" 3", 2);
Chris Lattner87f267e2006-11-21 05:02:33 +0000181 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000182 OutputString(" 3 4", 4);
183 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000184 }
185}
186
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000187
188/// FileChanged - Whenever the preprocessor enters or exits a #include file
189/// it invokes this handler. Update our conception of the current source
190/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000191void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
192 FileChangeReason Reason,
193 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000194 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000195
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000196 // Unless we are exiting a #include, make sure to skip ahead to the line the
197 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000198 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000199 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000200 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000201 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000202 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000203
204 // TODO GCC emits the # directive for this directive on the line AFTER the
205 // directive and emits a bunch of spaces that aren't needed. Emulate this
206 // strange behavior.
207 }
208
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000209 Loc = SourceMgr.getLogicalLoc(Loc);
Chris Lattner87f267e2006-11-21 05:02:33 +0000210 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner9b796242007-07-22 06:38:50 +0000211 CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
Chris Lattner87f267e2006-11-21 05:02:33 +0000212 FileType = FileType;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000213
Chris Lattner87f267e2006-11-21 05:02:33 +0000214 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000215 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000216 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000217 }
218
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000219 OutputChar('#');
220 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000221 std::string Num = llvm::utostr_32(CurLine);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000222 OutputString(&Num[0], Num.size());
223 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000224 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000225 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000226 OutputChar('"');
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000227
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000228 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000229 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000230 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000231 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000232 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000233 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000234 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000235 case PPCallbacks::SystemHeaderPragma: break;
236 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000237 }
238
239 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000240 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000241 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000242 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000243
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000244 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000245}
246
Chris Lattner728b4dc2006-07-04 21:28:37 +0000247/// HandleIdent - Handle #ident directives when read by the preprocessor.
248///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000249void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000250 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000251
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000252 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000253 OutputString(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000254 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000255}
256
257/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
258/// is called for the first token on each new line.
Chris Lattner146762e2007-07-20 16:59:19 +0000259void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000260 // Figure out what line we went to and insert the appropriate number of
261 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000262 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000263
264 // Print out space characters so that the first token on a line is
265 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000266 const SourceManager &SourceMgr = PP.getSourceManager();
267 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000268
269 // This hack prevents stuff like:
270 // #define HASH #
271 // HASH define foo bar
272 // From having the # character end up at column 1, which makes it so it
273 // is not handled as a #define next time through the preprocessor if in
274 // -fpreprocessed mode.
275 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000276 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000277
278 // Otherwise, indent the appropriate number of spaces.
279 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000280 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000281}
282
Chris Lattner5de858c2006-07-04 19:04:44 +0000283namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000284struct UnknownPragmaHandler : public PragmaHandler {
285 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000286 PrintPPOutputPPCallbacks *Callbacks;
287
288 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
289 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000290 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000291 // Figure out what line we went to and insert the appropriate number of
292 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000293 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000294 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000295
296 // Read and print all of the pragma tokens.
297 while (PragmaTok.getKind() != tok::eom) {
298 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000299 OutputChar(' ');
300 std::string TokSpell = PP.getSpelling(PragmaTok);
301 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000302 PP.LexUnexpandedToken(PragmaTok);
303 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000304 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000305 }
306};
Chris Lattner5de858c2006-07-04 19:04:44 +0000307} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000308
Chris Lattner4418ce12007-07-23 06:09:34 +0000309
310enum AvoidConcatInfo {
311 /// By default, a token never needs to avoid concatenation. Most tokens (e.g.
312 /// ',', ')', etc) don't cause a problem when concatenated.
313 aci_never_avoid_concat = 0,
314
315 /// aci_custom_firstchar - AvoidConcat contains custom code to handle this
316 /// token's requirements, and it needs to know the first character of the
317 /// token.
318 aci_custom_firstchar = 1,
319
320 /// aci_custom - AvoidConcat contains custom code to handle this token's
321 /// requirements, but it doesn't need to know the first character of the
322 /// token.
323 aci_custom = 2,
324
325 /// aci_avoid_equal - Many tokens cannot be safely followed by an '='
326 /// character. For example, "<<" turns into "<<=" when followed by an =.
327 aci_avoid_equal = 4
328};
329
330/// This array contains information for each token on what action to take when
331/// avoiding concatenation of tokens in the AvoidConcat method.
332static char TokenInfo[tok::NUM_TOKENS];
333
334/// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be
335/// marked by this function.
336static void InitAvoidConcatTokenInfo() {
337 // These tokens have custom code in AvoidConcat.
338 TokenInfo[tok::identifier ] |= aci_custom;
339 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
340 TokenInfo[tok::period ] |= aci_custom_firstchar;
341 TokenInfo[tok::amp ] |= aci_custom_firstchar;
342 TokenInfo[tok::plus ] |= aci_custom_firstchar;
343 TokenInfo[tok::minus ] |= aci_custom_firstchar;
344 TokenInfo[tok::slash ] |= aci_custom_firstchar;
345 TokenInfo[tok::less ] |= aci_custom_firstchar;
346 TokenInfo[tok::greater ] |= aci_custom_firstchar;
347 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
348 TokenInfo[tok::percent ] |= aci_custom_firstchar;
349 TokenInfo[tok::colon ] |= aci_custom_firstchar;
350 TokenInfo[tok::hash ] |= aci_custom_firstchar;
351 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
352
353 // These tokens change behavior if followed by an '='.
354 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
355 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
356 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
357 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
358 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
359 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
360 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
361 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
362 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
363 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
364 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
365 TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>=
366 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
367 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
368}
369
Chris Lattner331ad772006-07-28 06:56:01 +0000370/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
371/// the two individual tokens to be lexed as a single token, return true (which
372/// causes a space to be printed between them). This allows the output of -E
373/// mode to be lexed to the same token stream as lexing the input directly
374/// would.
375///
376/// This code must conservatively return true if it doesn't want to be 100%
377/// accurate. This will cause the output to include extra space characters, but
378/// the resulting output won't have incorrect concatenations going on. Examples
379/// include "..", which we print with a space between, because we don't want to
380/// track enough to tell "x.." from "...".
Chris Lattner146762e2007-07-20 16:59:19 +0000381bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
382 const Token &Tok) {
Chris Lattner331ad772006-07-28 06:56:01 +0000383 char Buffer[256];
384
Chris Lattner4418ce12007-07-23 06:09:34 +0000385 tok::TokenKind PrevKind = PrevTok.getKind();
386 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
387 PrevKind = tok::identifier;
388
389 // Look up information on when we should avoid concatenation with prevtok.
390 unsigned ConcatInfo = TokenInfo[PrevKind];
391
392 // If prevtok never causes a problem for anything after it, return quickly.
393 if (ConcatInfo == 0) return false;
Chris Lattner331ad772006-07-28 06:56:01 +0000394
Chris Lattner4418ce12007-07-23 06:09:34 +0000395 if (ConcatInfo & aci_avoid_equal) {
396 // If the next token is '=' or '==', avoid concatenation.
397 if (Tok.getKind() == tok::equal ||
398 Tok.getKind() == tok::equalequal)
399 return true;
400 ConcatInfo &= ~ConcatInfo;
401 }
402
403 if (ConcatInfo == 0) return false;
404
405
406
Chris Lattner331ad772006-07-28 06:56:01 +0000407 // Basic algorithm: we look at the first character of the second token, and
408 // determine whether it, if appended to the first token, would form (or would
409 // contribute) to a larger token if concatenated.
Chris Lattner4418ce12007-07-23 06:09:34 +0000410 char FirstChar = 0;
411 if (ConcatInfo & aci_custom) {
412 // If the token does not need to know the first character, don't get it.
413 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner331ad772006-07-28 06:56:01 +0000414 // Avoid spelling identifiers, the most common form of token.
415 FirstChar = II->getName()[0];
Chris Lattnere4c566c2007-07-23 05:18:42 +0000416 } else if (!Tok.needsCleaning()) {
417 SourceManager &SrcMgr = PP.getSourceManager();
418 FirstChar =
419 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Chris Lattner331ad772006-07-28 06:56:01 +0000420 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000421 const char *TokPtr = Buffer;
422 PP.getSpelling(Tok, TokPtr);
423 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000424 } else {
425 FirstChar = PP.getSpelling(Tok)[0];
426 }
Chris Lattner4418ce12007-07-23 06:09:34 +0000427
Chris Lattner331ad772006-07-28 06:56:01 +0000428 switch (PrevKind) {
Chris Lattner4418ce12007-07-23 06:09:34 +0000429 default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
Chris Lattner331ad772006-07-28 06:56:01 +0000430 case tok::identifier: // id+id or id+number or id+L"foo".
Chris Lattner4418ce12007-07-23 06:09:34 +0000431 if (Tok.getKind() == tok::numeric_constant || Tok.getIdentifierInfo() ||
432 Tok.getKind() == tok::wide_string_literal /* ||
433 Tok.getKind() == tok::wide_char_literal*/)
434 return true;
435 if (Tok.getKind() != tok::char_constant)
436 return false;
437
438 // FIXME: need a wide_char_constant!
439 if (!Tok.needsCleaning()) {
440 SourceManager &SrcMgr = PP.getSourceManager();
441 return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
442 == 'L';
443 } else if (Tok.getLength() < 256) {
444 const char *TokPtr = Buffer;
445 PP.getSpelling(Tok, TokPtr);
446 return TokPtr[0] == 'L';
447 } else {
448 return PP.getSpelling(Tok)[0] == 'L';
449 }
Chris Lattner331ad772006-07-28 06:56:01 +0000450 case tok::numeric_constant:
451 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
452 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
453 case tok::period: // ..., .*, .1234
454 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
Chris Lattner4418ce12007-07-23 06:09:34 +0000455 case tok::amp: // &&
456 return FirstChar == '&';
457 case tok::plus: // ++
458 return FirstChar == '+';
459 case tok::minus: // --, ->, ->*
460 return FirstChar == '-' || FirstChar == '>';
461 case tok::slash: //, /*, //
462 return FirstChar == '*' || FirstChar == '/';
463 case tok::less: // <<, <<=, <:, <%
464 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
465 case tok::greater: // >>, >>=
466 return FirstChar == '>';
467 case tok::pipe: // ||
468 return FirstChar == '|';
469 case tok::percent: // %>, %:
470 return FirstChar == '>' || FirstChar == ':';
Chris Lattner331ad772006-07-28 06:56:01 +0000471 case tok::colon: // ::, :>
472 return FirstChar == ':' || FirstChar == '>';
473 case tok::hash: // ##, #@, %:%:
474 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000475 case tok::arrow: // ->*
476 return FirstChar == '*';
Chris Lattner331ad772006-07-28 06:56:01 +0000477 }
478}
479
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000480/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000481///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000482void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
Chris Lattner2ea9dd72006-11-21 06:18:11 +0000483 const LangOptions &Options) {
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000484 // Inform the preprocessor whether we want it to retain comments or not, due
485 // to -C or -CC.
486 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Chris Lattner457fc152006-07-29 06:30:25 +0000487
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000488 InitOutputBuffer();
Chris Lattner4418ce12007-07-23 06:09:34 +0000489 InitAvoidConcatTokenInfo();
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000490
Chris Lattner146762e2007-07-20 16:59:19 +0000491 Token Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000492 char Buffer[256];
Chris Lattner87f267e2006-11-21 05:02:33 +0000493 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
494 PP.setPPCallbacks(Callbacks);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000495
Chris Lattner87f267e2006-11-21 05:02:33 +0000496 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
497 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000498
499 // After we have configured the preprocessor, enter the main file.
500
501 // Start parsing the specified input file.
502 PP.EnterSourceFile(MainFileID, 0, true);
503
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000504 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000505 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000506 PP.Lex(Tok);
507
Chris Lattner67c38482006-07-04 23:24:26 +0000508 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000509 if (Tok.isAtStartOfLine()) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000510 Callbacks->HandleFirstTokOnLine(Tok);
Chris Lattner331ad772006-07-28 06:56:01 +0000511 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-07-23 06:09:34 +0000512 // If we haven't emitted a token on this line yet, PrevTok isn't
513 // useful to look at and no concatenation could happen anyway.
514 (!Callbacks->hasEmittedTokensOnThisLine() &&
515 // Don't print "-" next to "-", it would form "--".
516 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000517 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000518 }
519
520 if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000521 const char *TokPtr = Buffer;
522 unsigned Len = PP.getSpelling(Tok, TokPtr);
523 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000524 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000525 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000526 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000527 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000528 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000529 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000530 OutputChar('\n');
531
532 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000533}
534