blob: a67f94c813e334049bdbb5ec0f47090ba9c2f61b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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
Chris Lattnerd7038e12009-02-13 00:46:04 +000015#include "clang/Basic/SourceManager.h"
Ted Kremenekc2542b62009-03-31 18:58:14 +000016#include "clang-cc.h"
Chris Lattnerf73903a2009-02-06 06:45:26 +000017#include "clang/Lex/MacroInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Lex/PPCallbacks.h"
19#include "clang/Lex/Preprocessor.h"
20#include "clang/Lex/Pragma.h"
Chris Lattnerd7038e12009-02-13 00:46:04 +000021#include "clang/Lex/TokenConcatenation.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/SourceManager.h"
Chris Lattner5db17c92008-04-08 04:16:20 +000023#include "clang/Basic/Diagnostic.h"
Chris Lattnerd8e30832007-07-24 06:57:14 +000024#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5db17c92008-04-08 04:16:20 +000026#include "llvm/System/Path.h"
27#include "llvm/Support/CommandLine.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include "llvm/Config/config.h"
Chris Lattnerdceb6a72008-08-17 01:47:12 +000029#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000030#include <cstdio>
31using namespace clang;
32
Chris Lattnerd82df3a2009-04-12 01:56:53 +000033/// PrintMacroDefinition - Print a macro definition in a form that will be
34/// properly accepted back as a definition.
35static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
36 Preprocessor &PP, llvm::raw_ostream &OS) {
37 OS << "#define " << II.getName();
38
39 if (MI.isFunctionLike()) {
40 OS << '(';
41 if (MI.arg_empty())
42 ;
43 else if (MI.getNumArgs() == 1)
44 OS << (*MI.arg_begin())->getName();
45 else {
46 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
47 OS << (*AI++)->getName();
48 while (AI != E)
49 OS << ',' << (*AI++)->getName();
50 }
51
52 if (MI.isVariadic()) {
53 if (!MI.arg_empty())
54 OS << ',';
55 OS << "...";
56 }
57 OS << ')';
58 }
59
60 // GCC always emits a space, even if the macro body is empty. However, do not
61 // want to emit two spaces if the first token has a leading space.
62 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
63 OS << ' ';
64
65 llvm::SmallVector<char, 128> SpellingBuffer;
66 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
67 I != E; ++I) {
68 if (I->hasLeadingSpace())
69 OS << ' ';
70
71 // Make sure we have enough space in the spelling buffer.
72 if (I->getLength() < SpellingBuffer.size())
73 SpellingBuffer.resize(I->getLength());
74 const char *Buffer = &SpellingBuffer[0];
75 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
76 OS.write(Buffer, SpellingLen);
77 }
78}
79
Reid Spencer5f016e22007-07-11 17:01:13 +000080//===----------------------------------------------------------------------===//
81// Preprocessed token printer
82//===----------------------------------------------------------------------===//
83
84static llvm::cl::opt<bool>
85DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
86static llvm::cl::opt<bool>
87EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
88static llvm::cl::opt<bool>
89EnableMacroCommentOutput("CC",
90 llvm::cl::desc("Enable comment output in -E mode, "
91 "even from macro expansions"));
Chris Lattnerf73903a2009-02-06 06:45:26 +000092static llvm::cl::opt<bool>
93DumpMacros("dM", llvm::cl::desc("Print macro definitions in -E mode instead of"
94 " normal output"));
95
Chris Lattnerd82df3a2009-04-12 01:56:53 +000096static llvm::cl::opt<bool>
97DumpDefines("dD", llvm::cl::desc("Print macro definitions in -E mode in "
98 "addition to normal output"));
Reid Spencer5f016e22007-07-11 17:01:13 +000099
100namespace {
101class PrintPPOutputPPCallbacks : public PPCallbacks {
102 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +0000103 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +0000104public:
105 llvm::raw_ostream &OS;
106private:
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 bool EmittedTokensOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +0000109 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000110 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000111 bool Initialized;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112public:
Chris Lattnere96de3e2008-08-17 03:12:02 +0000113 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os)
Chris Lattnerd7038e12009-02-13 00:46:04 +0000114 : PP(pp), ConcatInfo(PP), OS(os) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000116 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 EmittedTokensOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000118 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000119 Initialized = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121
122 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000123 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
125 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000126 SrcMgr::CharacteristicKind FileType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 virtual void Ident(SourceLocation Loc, const std::string &str);
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000128 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
129 const std::string &Str);
130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
Chris Lattner5f180322007-12-09 21:11:08 +0000132 bool HandleFirstTokOnLine(Token &Tok);
133 bool MoveToLine(SourceLocation Loc);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000134 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
135 return ConcatInfo.AvoidConcat(PrevTok, Tok);
136 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000137 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000138
139 /// MacroDefined - This hook is called whenever a macro definition is seen.
140 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142};
Chris Lattner5db17c92008-04-08 04:16:20 +0000143} // end anonymous namespace
Reid Spencer5f016e22007-07-11 17:01:13 +0000144
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000145void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
146 const char *Extra,
147 unsigned ExtraLen) {
148 if (EmittedTokensOnThisLine) {
149 OS << '\n';
150 EmittedTokensOnThisLine = false;
151 }
152
153 OS << '#' << ' ' << LineNo << ' ' << '"';
154 OS.write(&CurFilename[0], CurFilename.size());
155 OS << '"';
156
157 if (ExtraLen)
158 OS.write(Extra, ExtraLen);
159
Chris Lattner0b9e7362008-09-26 21:18:42 +0000160 if (FileType == SrcMgr::C_System)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000161 OS.write(" 3", 2);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000162 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000163 OS.write(" 3 4", 4);
164 OS << '\n';
165}
166
Reid Spencer5f016e22007-07-11 17:01:13 +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
Chris Lattner5f180322007-12-09 21:11:08 +0000169/// #line directive. This returns false if already at the specified line, true
170/// if some newlines were emitted.
171bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000172 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000175 if (LineNo == CurLine) return false;
176
177 CurLine = LineNo;
178
179 if (!EmittedTokensOnThisLine)
180 return true;
181
Chris Lattnere96de3e2008-08-17 03:12:02 +0000182 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000183 EmittedTokensOnThisLine = false;
184 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // If this line is "close enough" to the original line, just print newlines,
188 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000189 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000190 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000191 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000192 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000193 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000194 else {
195 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000196 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000197 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000199 WriteLineInfo(LineNo, 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000201
202 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000203 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000204}
205
206
207/// FileChanged - Whenever the preprocessor enters or exits a #include file
208/// it invokes this handler. Update our conception of the current source
209/// position.
210void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
211 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000212 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // Unless we are exiting a #include, make sure to skip ahead to the line the
214 // #include directive was at.
215 SourceManager &SourceMgr = PP.getSourceManager();
216 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000217 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
218 if (IncludeLoc.isValid())
219 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
221 MoveToLine(Loc);
222
223 // TODO GCC emits the # directive for this directive on the line AFTER the
224 // directive and emits a bunch of spaces that aren't needed. Emulate this
225 // strange behavior.
226 }
227
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000228 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000229 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000230 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000231
Chris Lattner5f180322007-12-09 21:11:08 +0000232 if (DisableLineMarkers) return;
233
Chris Lattnerd8e30832007-07-24 06:57:14 +0000234 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000235 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000236 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000237 FileType = NewFileType;
238
239 if (!Initialized) {
240 WriteLineInfo(CurLine);
241 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244 switch (Reason) {
245 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000246 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 break;
248 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000249 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 break;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000251 case PPCallbacks::SystemHeaderPragma:
252 case PPCallbacks::RenameFile:
253 WriteLineInfo(CurLine);
254 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000256}
257
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000258/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000259///
260void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
261 MoveToLine(Loc);
262
Chris Lattnere96de3e2008-08-17 03:12:02 +0000263 OS.write("#ident ", strlen("#ident "));
264 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 EmittedTokensOnThisLine = true;
266}
267
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000268/// MacroDefined - This hook is called whenever a macro definition is seen.
269void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
270 const MacroInfo *MI) {
271 // Only print out macro definitions in -dD mode.
272 if (!DumpDefines ||
273 // Ignore __FILE__ etc.
274 MI->isBuiltinMacro()) return;
275
276 MoveToLine(MI->getDefinitionLoc());
277 PrintMacroDefinition(*II, *MI, PP, OS);
278}
279
280
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000281void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
282 const IdentifierInfo *Kind,
283 const std::string &Str) {
284 MoveToLine(Loc);
285 OS << "#pragma comment(" << Kind->getName();
286
287 if (!Str.empty()) {
288 OS << ", \"";
289
290 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
291 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000292 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000293 OS << (char)Char;
294 else // Output anything hard as an octal escape.
295 OS << '\\'
296 << (char)('0'+ ((Char >> 6) & 7))
297 << (char)('0'+ ((Char >> 3) & 7))
298 << (char)('0'+ ((Char >> 0) & 7));
299 }
300 OS << '"';
301 }
302
303 OS << ')';
304 EmittedTokensOnThisLine = true;
305}
306
307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000309/// is called for the first token on each new line. If this really is the start
310/// of a new logical line, handle it and return true, otherwise return false.
311/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000312/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000313bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Figure out what line we went to and insert the appropriate number of
315 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000316 if (!MoveToLine(Tok.getLocation()))
317 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000318
319 // Print out space characters so that the first token on a line is
320 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000321 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000322 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000323
324 // This hack prevents stuff like:
325 // #define HASH #
326 // HASH define foo bar
327 // From having the # character end up at column 1, which makes it so it
328 // is not handled as a #define next time through the preprocessor if in
329 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000330 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000331 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000332
333 // Otherwise, indent the appropriate number of spaces.
334 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000335 OS << ' ';
Chris Lattner5f180322007-12-09 21:11:08 +0000336
337 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000338}
339
340namespace {
341struct UnknownPragmaHandler : public PragmaHandler {
342 const char *Prefix;
343 PrintPPOutputPPCallbacks *Callbacks;
344
345 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
346 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000347 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // Figure out what line we went to and insert the appropriate number of
349 // newline characters.
350 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000351 Callbacks->OS.write(Prefix, strlen(Prefix));
Reid Spencer5f016e22007-07-11 17:01:13 +0000352
353 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000354 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000356 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000358 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 PP.LexUnexpandedToken(PragmaTok);
360 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000361 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 }
363};
364} // end anonymous namespace
365
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000366
Chris Lattner59076ab2009-02-06 05:56:11 +0000367static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
368 PrintPPOutputPPCallbacks *Callbacks,
369 llvm::raw_ostream &OS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000371 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000372 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000373
374 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000375 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
376 // done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000378 // If we haven't emitted a token on this line yet, PrevTok isn't
379 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000380 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000381 // Don't print "-" next to "-", it would form "--".
382 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000383 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 }
385
Chris Lattner2933f412007-07-23 06:14:36 +0000386 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner33116d62009-01-26 19:33:54 +0000387 OS.write(II->getName(), II->getLength());
388 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
389 Tok.getLiteralData()) {
390 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner2933f412007-07-23 06:14:36 +0000391 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 const char *TokPtr = Buffer;
393 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000394 OS.write(TokPtr, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 } else {
396 std::string S = PP.getSpelling(Tok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000397 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
399 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner6f688e12007-10-10 20:45:16 +0000400
401 if (Tok.is(tok::eof)) break;
Chris Lattner59076ab2009-02-06 05:56:11 +0000402
Chris Lattner6f688e12007-10-10 20:45:16 +0000403 PrevTok = Tok;
404 PP.Lex(Tok);
405 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000406}
407
Chris Lattner2a2bb182009-02-10 22:28:19 +0000408namespace {
409 struct SortMacrosByID {
410 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
411 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
412 return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
413 }
414 };
415}
Chris Lattner59076ab2009-02-06 05:56:11 +0000416
417/// DoPrintPreprocessedInput - This implements -E mode.
418///
419void clang::DoPrintPreprocessedInput(Preprocessor &PP,
Eli Friedmanf54fce82009-05-19 01:02:07 +0000420 llvm::raw_ostream *OS) {
Chris Lattner59076ab2009-02-06 05:56:11 +0000421 // Inform the preprocessor whether we want it to retain comments or not, due
422 // to -C or -CC.
423 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000424
425 OS->SetBufferSize(64*1024);
Chris Lattner59076ab2009-02-06 05:56:11 +0000426
Chris Lattnerf73903a2009-02-06 06:45:26 +0000427 if (DumpMacros) {
428 // -dM mode just scans and ignores all tokens in the files, then dumps out
429 // the macro table at the end.
430 PP.EnterMainSourceFile();
431
432 Token Tok;
433 do PP.Lex(Tok);
434 while (Tok.isNot(tok::eof));
435
Chris Lattner2a2bb182009-02-10 22:28:19 +0000436 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
Chris Lattnerf73903a2009-02-06 06:45:26 +0000437 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
438 I != E; ++I)
Chris Lattner2a2bb182009-02-10 22:28:19 +0000439 MacrosByID.push_back(*I);
440 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
441
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000442 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
443 MacroInfo &MI = *MacrosByID[i].second;
444 // Ignore computed macros like __LINE__ and friends.
445 if (MI.isBuiltinMacro()) continue;
446
Eli Friedmanf54fce82009-05-19 01:02:07 +0000447 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
448 *OS << "\n";
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000449 }
Chris Lattnerf73903a2009-02-06 06:45:26 +0000450
451 } else {
Eli Friedmanf54fce82009-05-19 01:02:07 +0000452 PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, *OS);
Chris Lattnerf73903a2009-02-06 06:45:26 +0000453 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
454 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
455 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000456
Chris Lattnerf73903a2009-02-06 06:45:26 +0000457 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000458
Chris Lattnerf73903a2009-02-06 06:45:26 +0000459 // After we have configured the preprocessor, enter the main file.
460 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000461
Chris Lattnerf73903a2009-02-06 06:45:26 +0000462 // Consume all of the tokens that come from the predefines buffer. Those
463 // should not be emitted into the output and are guaranteed to be at the
464 // start.
465 const SourceManager &SourceMgr = PP.getSourceManager();
466 Token Tok;
467 do PP.Lex(Tok);
468 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
469 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
Chris Lattner7d66ebb2009-03-20 21:23:42 +0000470 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000471
Chris Lattnerf73903a2009-02-06 06:45:26 +0000472 // Read all the preprocessed tokens, printing them out to the stream.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000473 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
474 *OS << '\n';
Chris Lattnerf73903a2009-02-06 06:45:26 +0000475 }
Eli Friedmanf54fce82009-05-19 01:02:07 +0000476
Chris Lattner76b3a722008-08-17 07:07:01 +0000477 // Flush the ostream.
Eli Friedmanf54fce82009-05-19 01:02:07 +0000478 OS->flush();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479}
480