blob: a8bc4e40d222dde426682a55f6bd8116b6dd918a [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 Lattner4c4a2452007-07-24 06:57:14 +000021#include "llvm/ADT/SmallString.h"
Chris Lattnerf46be6c2006-07-04 22:19:33 +000022#include "llvm/ADT/StringExtras.h"
23#include "llvm/Config/config.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000024#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000025using 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();
Chris Lattner93c4ea72007-07-23 06:23:07 +000091
92 switch (Size) {
93 default:
94 memcpy(OutBufCur, Ptr, Size);
95 break;
96 case 3:
97 OutBufCur[2] = Ptr[2];
98 case 2:
99 OutBufCur[1] = Ptr[1];
100 case 1:
101 OutBufCur[0] = Ptr[0];
102 case 0:
103 break;
104 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000105 OutBufCur += Size;
106#endif
107}
108
109
110//===----------------------------------------------------------------------===//
111// Preprocessed token printer
112//===----------------------------------------------------------------------===//
113
Chris Lattner23b7eb62007-06-15 23:05:46 +0000114static llvm::cl::opt<bool>
115DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
116static llvm::cl::opt<bool>
117EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
118static llvm::cl::opt<bool>
119EnableMacroCommentOutput("CC",
120 llvm::cl::desc("Enable comment output in -E mode, "
Chris Lattner457fc152006-07-29 06:30:25 +0000121 "even from macro expansions"));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000122
Chris Lattner87f267e2006-11-21 05:02:33 +0000123namespace {
124class PrintPPOutputPPCallbacks : public PPCallbacks {
125 Preprocessor &PP;
126 unsigned CurLine;
Chris Lattner87f267e2006-11-21 05:02:33 +0000127 bool EmittedTokensOnThisLine;
128 DirectoryLookup::DirType FileType;
Chris Lattner4c4a2452007-07-24 06:57:14 +0000129 llvm::SmallString<512> CurFilename;
Chris Lattner87f267e2006-11-21 05:02:33 +0000130public:
131 PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) {
132 CurLine = 0;
Chris Lattner4c4a2452007-07-24 06:57:14 +0000133 CurFilename += "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000134 EmittedTokensOnThisLine = false;
135 FileType = DirectoryLookup::NormalHeaderDir;
136 }
137
138 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000139 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Chris Lattner87f267e2006-11-21 05:02:33 +0000140
141 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
142 DirectoryLookup::DirType FileType);
143 virtual void Ident(SourceLocation Loc, const std::string &str);
144
145
Chris Lattner146762e2007-07-20 16:59:19 +0000146 void HandleFirstTokOnLine(Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000147 void MoveToLine(SourceLocation Loc);
Chris Lattner146762e2007-07-20 16:59:19 +0000148 bool AvoidConcat(const Token &PrevTok, const Token &Tok);
Chris Lattner87f267e2006-11-21 05:02:33 +0000149};
150}
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000151
Chris Lattner5cdfebb2007-07-23 06:31:11 +0000152/// UToStr - Do itoa on the specified number, in-place in the specified buffer.
153/// endptr points to the end of the buffer.
154static char *UToStr(unsigned N, char *EndPtr) {
155 // Null terminate the buffer.
156 *--EndPtr = '\0';
157 if (N == 0) // Zero is a special case.
158 *--EndPtr = '0';
159 while (N) {
160 *--EndPtr = '0' + char(N % 10);
161 N /= 10;
162 }
163 return EndPtr;
164}
165
166
Chris Lattner728b4dc2006-07-04 21:28:37 +0000167/// MoveToLine - Move the output to the source line specified by the location
168/// object. We can do this by emitting some number of \n's, or be emitting a
169/// #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000170void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000171 if (DisableLineMarkers) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000172 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000173 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000174 EmittedTokensOnThisLine = false;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000175 }
176 return;
177 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000178
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000179 unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
Chris Lattner3338ba82006-07-04 21:19:39 +0000180
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000181 // If this line is "close enough" to the original line, just print newlines,
182 // otherwise print a #line directive.
Chris Lattner87f267e2006-11-21 05:02:33 +0000183 if (LineNo-CurLine < 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000184 if (LineNo-CurLine == 1)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000185 OutputChar('\n');
Chris Lattner5f075822007-07-23 05:14:05 +0000186 else {
187 const char *NewLines = "\n\n\n\n\n\n\n\n";
188 OutputString(NewLines, LineNo-CurLine);
189 CurLine = LineNo;
190 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000191 } else {
Chris Lattner87f267e2006-11-21 05:02:33 +0000192 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000193 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000194 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000195 }
196
Chris Lattner87f267e2006-11-21 05:02:33 +0000197 CurLine = LineNo;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000198
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000199 OutputChar('#');
200 OutputChar(' ');
Chris Lattner5cdfebb2007-07-23 06:31:11 +0000201 char NumberBuffer[20];
202 const char *NumStr = UToStr(LineNo, NumberBuffer+20);
203 OutputString(NumStr, (NumberBuffer+20)-NumStr-1);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000204 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000205 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000206 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000207 OutputChar('"');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000208
Chris Lattner87f267e2006-11-21 05:02:33 +0000209 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000210 OutputString(" 3", 2);
Chris Lattner87f267e2006-11-21 05:02:33 +0000211 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000212 OutputString(" 3 4", 4);
213 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000214 }
215}
216
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000217
218/// FileChanged - Whenever the preprocessor enters or exits a #include file
219/// it invokes this handler. Update our conception of the current source
220/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000221void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
222 FileChangeReason Reason,
223 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +0000224 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +0000225
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000226 // Unless we are exiting a #include, make sure to skip ahead to the line the
227 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000228 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000229 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000230 MoveToLine(SourceMgr.getIncludeLoc(Loc));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000231 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000232 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000233
234 // TODO GCC emits the # directive for this directive on the line AFTER the
235 // directive and emits a bunch of spaces that aren't needed. Emulate this
236 // strange behavior.
237 }
238
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000239 Loc = SourceMgr.getLogicalLoc(Loc);
Chris Lattner87f267e2006-11-21 05:02:33 +0000240 CurLine = SourceMgr.getLineNumber(Loc);
Chris Lattner4c4a2452007-07-24 06:57:14 +0000241 CurFilename.clear();
242 CurFilename += SourceMgr.getSourceName(Loc);
243 Lexer::Stringify(CurFilename);
Chris Lattner87f267e2006-11-21 05:02:33 +0000244 FileType = FileType;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000245
Chris Lattner87f267e2006-11-21 05:02:33 +0000246 if (EmittedTokensOnThisLine) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000247 OutputChar('\n');
Chris Lattner87f267e2006-11-21 05:02:33 +0000248 EmittedTokensOnThisLine = false;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000249 }
250
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000251 OutputChar('#');
252 OutputChar(' ');
Chris Lattner23b7eb62007-06-15 23:05:46 +0000253 std::string Num = llvm::utostr_32(CurLine);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000254 OutputString(&Num[0], Num.size());
255 OutputChar(' ');
Chris Lattner9b796242007-07-22 06:38:50 +0000256 OutputChar('"');
Chris Lattner87f267e2006-11-21 05:02:33 +0000257 OutputString(&CurFilename[0], CurFilename.size());
Chris Lattner9b796242007-07-22 06:38:50 +0000258 OutputChar('"');
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000259
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000260 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000261 case PPCallbacks::EnterFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000262 OutputString(" 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000263 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000264 case PPCallbacks::ExitFile:
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000265 OutputString(" 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000266 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000267 case PPCallbacks::SystemHeaderPragma: break;
268 case PPCallbacks::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000269 }
270
271 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000272 OutputString(" 3", 2);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000273 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000274 OutputString(" 3 4", 4);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000275
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000276 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000277}
278
Chris Lattner728b4dc2006-07-04 21:28:37 +0000279/// HandleIdent - Handle #ident directives when read by the preprocessor.
280///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000281void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000282 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000283
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000284 OutputString("#ident ", strlen("#ident "));
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000285 OutputString(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000286 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000287}
288
289/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
290/// is called for the first token on each new line.
Chris Lattner146762e2007-07-20 16:59:19 +0000291void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000292 // Figure out what line we went to and insert the appropriate number of
293 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000294 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000295
296 // Print out space characters so that the first token on a line is
297 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000298 const SourceManager &SourceMgr = PP.getSourceManager();
299 unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000300
301 // This hack prevents stuff like:
302 // #define HASH #
303 // HASH define foo bar
304 // From having the # character end up at column 1, which makes it so it
305 // is not handled as a #define next time through the preprocessor if in
306 // -fpreprocessed mode.
307 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000308 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000309
310 // Otherwise, indent the appropriate number of spaces.
311 for (; ColNo > 1; --ColNo)
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000312 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000313}
314
Chris Lattner5de858c2006-07-04 19:04:44 +0000315namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000316struct UnknownPragmaHandler : public PragmaHandler {
317 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000318 PrintPPOutputPPCallbacks *Callbacks;
319
320 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
321 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000322 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000323 // Figure out what line we went to and insert the appropriate number of
324 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000325 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000326 OutputString(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000327
328 // Read and print all of the pragma tokens.
329 while (PragmaTok.getKind() != tok::eom) {
330 if (PragmaTok.hasLeadingSpace())
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000331 OutputChar(' ');
332 std::string TokSpell = PP.getSpelling(PragmaTok);
333 OutputString(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000334 PP.LexUnexpandedToken(PragmaTok);
335 }
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000336 OutputChar('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000337 }
338};
Chris Lattner5de858c2006-07-04 19:04:44 +0000339} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000340
Chris Lattner4418ce12007-07-23 06:09:34 +0000341
342enum AvoidConcatInfo {
343 /// By default, a token never needs to avoid concatenation. Most tokens (e.g.
344 /// ',', ')', etc) don't cause a problem when concatenated.
345 aci_never_avoid_concat = 0,
346
347 /// aci_custom_firstchar - AvoidConcat contains custom code to handle this
348 /// token's requirements, and it needs to know the first character of the
349 /// token.
350 aci_custom_firstchar = 1,
351
352 /// aci_custom - AvoidConcat contains custom code to handle this token's
353 /// requirements, but it doesn't need to know the first character of the
354 /// token.
355 aci_custom = 2,
356
357 /// aci_avoid_equal - Many tokens cannot be safely followed by an '='
358 /// character. For example, "<<" turns into "<<=" when followed by an =.
359 aci_avoid_equal = 4
360};
361
362/// This array contains information for each token on what action to take when
363/// avoiding concatenation of tokens in the AvoidConcat method.
364static char TokenInfo[tok::NUM_TOKENS];
365
366/// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be
367/// marked by this function.
368static void InitAvoidConcatTokenInfo() {
369 // These tokens have custom code in AvoidConcat.
370 TokenInfo[tok::identifier ] |= aci_custom;
371 TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
372 TokenInfo[tok::period ] |= aci_custom_firstchar;
373 TokenInfo[tok::amp ] |= aci_custom_firstchar;
374 TokenInfo[tok::plus ] |= aci_custom_firstchar;
375 TokenInfo[tok::minus ] |= aci_custom_firstchar;
376 TokenInfo[tok::slash ] |= aci_custom_firstchar;
377 TokenInfo[tok::less ] |= aci_custom_firstchar;
378 TokenInfo[tok::greater ] |= aci_custom_firstchar;
379 TokenInfo[tok::pipe ] |= aci_custom_firstchar;
380 TokenInfo[tok::percent ] |= aci_custom_firstchar;
381 TokenInfo[tok::colon ] |= aci_custom_firstchar;
382 TokenInfo[tok::hash ] |= aci_custom_firstchar;
383 TokenInfo[tok::arrow ] |= aci_custom_firstchar;
384
385 // These tokens change behavior if followed by an '='.
386 TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
387 TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
388 TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
389 TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
390 TokenInfo[tok::less ] |= aci_avoid_equal; // <=
391 TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
392 TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
393 TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
394 TokenInfo[tok::star ] |= aci_avoid_equal; // *=
395 TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
396 TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
397 TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>=
398 TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
399 TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
400}
401
Chris Lattner331ad772006-07-28 06:56:01 +0000402/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
403/// the two individual tokens to be lexed as a single token, return true (which
404/// causes a space to be printed between them). This allows the output of -E
405/// mode to be lexed to the same token stream as lexing the input directly
406/// would.
407///
408/// This code must conservatively return true if it doesn't want to be 100%
409/// accurate. This will cause the output to include extra space characters, but
410/// the resulting output won't have incorrect concatenations going on. Examples
411/// include "..", which we print with a space between, because we don't want to
412/// track enough to tell "x.." from "...".
Chris Lattner146762e2007-07-20 16:59:19 +0000413bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
414 const Token &Tok) {
Chris Lattner331ad772006-07-28 06:56:01 +0000415 char Buffer[256];
416
Chris Lattner4418ce12007-07-23 06:09:34 +0000417 tok::TokenKind PrevKind = PrevTok.getKind();
418 if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
419 PrevKind = tok::identifier;
420
421 // Look up information on when we should avoid concatenation with prevtok.
422 unsigned ConcatInfo = TokenInfo[PrevKind];
423
424 // If prevtok never causes a problem for anything after it, return quickly.
425 if (ConcatInfo == 0) return false;
Chris Lattner331ad772006-07-28 06:56:01 +0000426
Chris Lattner4418ce12007-07-23 06:09:34 +0000427 if (ConcatInfo & aci_avoid_equal) {
428 // If the next token is '=' or '==', avoid concatenation.
429 if (Tok.getKind() == tok::equal ||
430 Tok.getKind() == tok::equalequal)
431 return true;
Chris Lattnerd63c8a52007-07-23 23:21:34 +0000432 ConcatInfo &= ~aci_avoid_equal;
Chris Lattner4418ce12007-07-23 06:09:34 +0000433 }
434
435 if (ConcatInfo == 0) return false;
436
437
438
Chris Lattner331ad772006-07-28 06:56:01 +0000439 // Basic algorithm: we look at the first character of the second token, and
440 // determine whether it, if appended to the first token, would form (or would
441 // contribute) to a larger token if concatenated.
Chris Lattner4418ce12007-07-23 06:09:34 +0000442 char FirstChar = 0;
443 if (ConcatInfo & aci_custom) {
444 // If the token does not need to know the first character, don't get it.
445 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner331ad772006-07-28 06:56:01 +0000446 // Avoid spelling identifiers, the most common form of token.
447 FirstChar = II->getName()[0];
Chris Lattnere4c566c2007-07-23 05:18:42 +0000448 } else if (!Tok.needsCleaning()) {
449 SourceManager &SrcMgr = PP.getSourceManager();
450 FirstChar =
451 *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
Chris Lattner331ad772006-07-28 06:56:01 +0000452 } else if (Tok.getLength() < 256) {
Chris Lattner9f547a42006-10-18 06:06:41 +0000453 const char *TokPtr = Buffer;
454 PP.getSpelling(Tok, TokPtr);
455 FirstChar = TokPtr[0];
Chris Lattner331ad772006-07-28 06:56:01 +0000456 } else {
457 FirstChar = PP.getSpelling(Tok)[0];
458 }
Chris Lattner4418ce12007-07-23 06:09:34 +0000459
Chris Lattner331ad772006-07-28 06:56:01 +0000460 switch (PrevKind) {
Chris Lattner4418ce12007-07-23 06:09:34 +0000461 default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
Chris Lattner331ad772006-07-28 06:56:01 +0000462 case tok::identifier: // id+id or id+number or id+L"foo".
Chris Lattner4418ce12007-07-23 06:09:34 +0000463 if (Tok.getKind() == tok::numeric_constant || Tok.getIdentifierInfo() ||
464 Tok.getKind() == tok::wide_string_literal /* ||
465 Tok.getKind() == tok::wide_char_literal*/)
466 return true;
467 if (Tok.getKind() != tok::char_constant)
468 return false;
469
470 // FIXME: need a wide_char_constant!
471 if (!Tok.needsCleaning()) {
472 SourceManager &SrcMgr = PP.getSourceManager();
473 return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
474 == 'L';
475 } else if (Tok.getLength() < 256) {
476 const char *TokPtr = Buffer;
477 PP.getSpelling(Tok, TokPtr);
478 return TokPtr[0] == 'L';
479 } else {
480 return PP.getSpelling(Tok)[0] == 'L';
481 }
Chris Lattner331ad772006-07-28 06:56:01 +0000482 case tok::numeric_constant:
483 return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
484 FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
485 case tok::period: // ..., .*, .1234
486 return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
Chris Lattner4418ce12007-07-23 06:09:34 +0000487 case tok::amp: // &&
488 return FirstChar == '&';
489 case tok::plus: // ++
490 return FirstChar == '+';
491 case tok::minus: // --, ->, ->*
492 return FirstChar == '-' || FirstChar == '>';
493 case tok::slash: //, /*, //
494 return FirstChar == '*' || FirstChar == '/';
495 case tok::less: // <<, <<=, <:, <%
496 return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
497 case tok::greater: // >>, >>=
498 return FirstChar == '>';
499 case tok::pipe: // ||
500 return FirstChar == '|';
501 case tok::percent: // %>, %:
502 return FirstChar == '>' || FirstChar == ':';
Chris Lattner331ad772006-07-28 06:56:01 +0000503 case tok::colon: // ::, :>
504 return FirstChar == ':' || FirstChar == '>';
505 case tok::hash: // ##, #@, %:%:
506 return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
Chris Lattner331ad772006-07-28 06:56:01 +0000507 case tok::arrow: // ->*
508 return FirstChar == '*';
Chris Lattner331ad772006-07-28 06:56:01 +0000509 }
510}
511
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000512/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000513///
Chris Lattnercd028fc2006-07-29 06:35:08 +0000514void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
Chris Lattner2ea9dd72006-11-21 06:18:11 +0000515 const LangOptions &Options) {
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000516 // Inform the preprocessor whether we want it to retain comments or not, due
517 // to -C or -CC.
518 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Chris Lattner457fc152006-07-29 06:30:25 +0000519
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000520 InitOutputBuffer();
Chris Lattner4418ce12007-07-23 06:09:34 +0000521 InitAvoidConcatTokenInfo();
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000522
Chris Lattner146762e2007-07-20 16:59:19 +0000523 Token Tok, PrevTok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000524 char Buffer[256];
Chris Lattner87f267e2006-11-21 05:02:33 +0000525 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
526 PP.setPPCallbacks(Callbacks);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000527
Chris Lattner87f267e2006-11-21 05:02:33 +0000528 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
529 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
Chris Lattnercd028fc2006-07-29 06:35:08 +0000530
531 // After we have configured the preprocessor, enter the main file.
532
533 // Start parsing the specified input file.
534 PP.EnterSourceFile(MainFileID, 0, true);
535
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000536 do {
Chris Lattner331ad772006-07-28 06:56:01 +0000537 PrevTok = Tok;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000538 PP.Lex(Tok);
539
Chris Lattner67c38482006-07-04 23:24:26 +0000540 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000541 if (Tok.isAtStartOfLine()) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000542 Callbacks->HandleFirstTokOnLine(Tok);
Chris Lattner331ad772006-07-28 06:56:01 +0000543 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-07-23 06:09:34 +0000544 // If we haven't emitted a token on this line yet, PrevTok isn't
545 // useful to look at and no concatenation could happen anyway.
Chris Lattnerd63c8a52007-07-23 23:21:34 +0000546 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattner4418ce12007-07-23 06:09:34 +0000547 // Don't print "-" next to "-", it would form "--".
548 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000549 OutputChar(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000550 }
551
Chris Lattner0af98232007-07-23 06:14:36 +0000552 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
553 const char *Str = II->getName();
554 unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength();
555 OutputString(Str, Len);
556 } else if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000557 const char *TokPtr = Buffer;
558 unsigned Len = PP.getSpelling(Tok, TokPtr);
559 OutputString(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000560 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000561 std::string S = PP.getSpelling(Tok);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000562 OutputString(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000563 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000564 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000565 } while (Tok.getKind() != tok::eof);
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000566 OutputChar('\n');
567
568 CleanupOutputBuffer();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000569}
570