blob: 6a7ad31b53ec73e755adbe2fb14a29907976f7c1 [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
Eli Friedmanb09f6e12009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Chris Lattnerf73903a2009-02-06 06:45:26 +000016#include "clang/Lex/MacroInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Lex/PPCallbacks.h"
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Lex/Pragma.h"
Chris Lattnerd7038e12009-02-13 00:46:04 +000020#include "clang/Lex/TokenConcatenation.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner5db17c92008-04-08 04:16:20 +000022#include "clang/Basic/Diagnostic.h"
Chris Lattnerd8e30832007-07-24 06:57:14 +000023#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner5db17c92008-04-08 04:16:20 +000025#include "llvm/System/Path.h"
26#include "llvm/Support/CommandLine.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include "llvm/Config/config.h"
Chris Lattnerdceb6a72008-08-17 01:47:12 +000028#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000029#include <cstdio>
30using namespace clang;
31
Chris Lattnerd82df3a2009-04-12 01:56:53 +000032/// PrintMacroDefinition - Print a macro definition in a form that will be
33/// properly accepted back as a definition.
34static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
35 Preprocessor &PP, llvm::raw_ostream &OS) {
36 OS << "#define " << II.getName();
37
38 if (MI.isFunctionLike()) {
39 OS << '(';
40 if (MI.arg_empty())
41 ;
42 else if (MI.getNumArgs() == 1)
43 OS << (*MI.arg_begin())->getName();
44 else {
45 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
46 OS << (*AI++)->getName();
47 while (AI != E)
48 OS << ',' << (*AI++)->getName();
49 }
50
51 if (MI.isVariadic()) {
52 if (!MI.arg_empty())
53 OS << ',';
54 OS << "...";
55 }
56 OS << ')';
57 }
58
59 // GCC always emits a space, even if the macro body is empty. However, do not
60 // want to emit two spaces if the first token has a leading space.
61 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
62 OS << ' ';
63
64 llvm::SmallVector<char, 128> SpellingBuffer;
65 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
66 I != E; ++I) {
67 if (I->hasLeadingSpace())
68 OS << ' ';
69
70 // Make sure we have enough space in the spelling buffer.
71 if (I->getLength() < SpellingBuffer.size())
72 SpellingBuffer.resize(I->getLength());
73 const char *Buffer = &SpellingBuffer[0];
74 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
75 OS.write(Buffer, SpellingLen);
76 }
77}
78
Reid Spencer5f016e22007-07-11 17:01:13 +000079//===----------------------------------------------------------------------===//
80// Preprocessed token printer
81//===----------------------------------------------------------------------===//
82
Reid Spencer5f016e22007-07-11 17:01:13 +000083namespace {
84class PrintPPOutputPPCallbacks : public PPCallbacks {
85 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +000086 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +000087public:
88 llvm::raw_ostream &OS;
89private:
Reid Spencer5f016e22007-07-11 17:01:13 +000090 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +000091 bool EmittedTokensOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +000092 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +000093 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +000094 bool Initialized;
Eli Friedman12d3b1d2009-05-19 03:06:47 +000095 bool DisableLineMarkers;
96 bool DumpDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +000097public:
Eli Friedman12d3b1d2009-05-19 03:06:47 +000098 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
99 bool lineMarkers, bool defines)
100 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
101 DumpDefines(defines) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000103 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 EmittedTokensOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000105 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000106 Initialized = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108
109 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000110 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000111
112 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000113 SrcMgr::CharacteristicKind FileType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 virtual void Ident(SourceLocation Loc, const std::string &str);
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000115 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
116 const std::string &Str);
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118
Chris Lattner5f180322007-12-09 21:11:08 +0000119 bool HandleFirstTokOnLine(Token &Tok);
120 bool MoveToLine(SourceLocation Loc);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000121 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
122 return ConcatInfo.AvoidConcat(PrevTok, Tok);
123 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000124 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000125
126 /// MacroDefined - This hook is called whenever a macro definition is seen.
127 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129};
Chris Lattner5db17c92008-04-08 04:16:20 +0000130} // end anonymous namespace
Reid Spencer5f016e22007-07-11 17:01:13 +0000131
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000132void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
133 const char *Extra,
134 unsigned ExtraLen) {
135 if (EmittedTokensOnThisLine) {
136 OS << '\n';
137 EmittedTokensOnThisLine = false;
138 }
139
140 OS << '#' << ' ' << LineNo << ' ' << '"';
141 OS.write(&CurFilename[0], CurFilename.size());
142 OS << '"';
143
144 if (ExtraLen)
145 OS.write(Extra, ExtraLen);
146
Chris Lattner0b9e7362008-09-26 21:18:42 +0000147 if (FileType == SrcMgr::C_System)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000148 OS.write(" 3", 2);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000149 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000150 OS.write(" 3 4", 4);
151 OS << '\n';
152}
153
Reid Spencer5f016e22007-07-11 17:01:13 +0000154/// MoveToLine - Move the output to the source line specified by the location
155/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner5f180322007-12-09 21:11:08 +0000156/// #line directive. This returns false if already at the specified line, true
157/// if some newlines were emitted.
158bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000159 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000160
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000162 if (LineNo == CurLine) return false;
163
164 CurLine = LineNo;
165
166 if (!EmittedTokensOnThisLine)
167 return true;
168
Chris Lattnere96de3e2008-08-17 03:12:02 +0000169 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000170 EmittedTokensOnThisLine = false;
171 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000173
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 // If this line is "close enough" to the original line, just print newlines,
175 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000176 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000177 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000178 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000179 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000180 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000181 else {
182 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000183 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000184 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000186 WriteLineInfo(LineNo, 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000188
189 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000190 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000191}
192
193
194/// FileChanged - Whenever the preprocessor enters or exits a #include file
195/// it invokes this handler. Update our conception of the current source
196/// position.
197void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
198 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000199 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 // Unless we are exiting a #include, make sure to skip ahead to the line the
201 // #include directive was at.
202 SourceManager &SourceMgr = PP.getSourceManager();
203 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000204 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
205 if (IncludeLoc.isValid())
206 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
208 MoveToLine(Loc);
209
210 // TODO GCC emits the # directive for this directive on the line AFTER the
211 // directive and emits a bunch of spaces that aren't needed. Emulate this
212 // strange behavior.
213 }
214
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000215 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000216 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000217 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000218
Chris Lattner5f180322007-12-09 21:11:08 +0000219 if (DisableLineMarkers) return;
220
Chris Lattnerd8e30832007-07-24 06:57:14 +0000221 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000222 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000223 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000224 FileType = NewFileType;
225
226 if (!Initialized) {
227 WriteLineInfo(CurLine);
228 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 switch (Reason) {
232 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000233 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 break;
235 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000236 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 break;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000238 case PPCallbacks::SystemHeaderPragma:
239 case PPCallbacks::RenameFile:
240 WriteLineInfo(CurLine);
241 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000243}
244
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000245/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000246///
247void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
248 MoveToLine(Loc);
249
Chris Lattnere96de3e2008-08-17 03:12:02 +0000250 OS.write("#ident ", strlen("#ident "));
251 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 EmittedTokensOnThisLine = true;
253}
254
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000255/// MacroDefined - This hook is called whenever a macro definition is seen.
256void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
257 const MacroInfo *MI) {
258 // Only print out macro definitions in -dD mode.
259 if (!DumpDefines ||
260 // Ignore __FILE__ etc.
261 MI->isBuiltinMacro()) return;
262
263 MoveToLine(MI->getDefinitionLoc());
264 PrintMacroDefinition(*II, *MI, PP, OS);
265}
266
267
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000268void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
269 const IdentifierInfo *Kind,
270 const std::string &Str) {
271 MoveToLine(Loc);
272 OS << "#pragma comment(" << Kind->getName();
273
274 if (!Str.empty()) {
275 OS << ", \"";
276
277 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
278 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000279 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000280 OS << (char)Char;
281 else // Output anything hard as an octal escape.
282 OS << '\\'
283 << (char)('0'+ ((Char >> 6) & 7))
284 << (char)('0'+ ((Char >> 3) & 7))
285 << (char)('0'+ ((Char >> 0) & 7));
286 }
287 OS << '"';
288 }
289
290 OS << ')';
291 EmittedTokensOnThisLine = true;
292}
293
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000296/// is called for the first token on each new line. If this really is the start
297/// of a new logical line, handle it and return true, otherwise return false.
298/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000299/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000300bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 // Figure out what line we went to and insert the appropriate number of
302 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000303 if (!MoveToLine(Tok.getLocation()))
304 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305
306 // Print out space characters so that the first token on a line is
307 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000308 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000309 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000310
311 // This hack prevents stuff like:
312 // #define HASH #
313 // HASH define foo bar
314 // From having the # character end up at column 1, which makes it so it
315 // is not handled as a #define next time through the preprocessor if in
316 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000317 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000318 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000319
320 // Otherwise, indent the appropriate number of spaces.
321 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000322 OS << ' ';
Chris Lattner5f180322007-12-09 21:11:08 +0000323
324 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000325}
326
327namespace {
328struct UnknownPragmaHandler : public PragmaHandler {
329 const char *Prefix;
330 PrintPPOutputPPCallbacks *Callbacks;
331
332 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
333 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000334 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 // Figure out what line we went to and insert the appropriate number of
336 // newline characters.
337 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000338 Callbacks->OS.write(Prefix, strlen(Prefix));
Reid Spencer5f016e22007-07-11 17:01:13 +0000339
340 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000341 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000343 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000345 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 PP.LexUnexpandedToken(PragmaTok);
347 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000348 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 }
350};
351} // end anonymous namespace
352
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000353
Chris Lattner59076ab2009-02-06 05:56:11 +0000354static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
355 PrintPPOutputPPCallbacks *Callbacks,
356 llvm::raw_ostream &OS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000358 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000359 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000360
361 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000362 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
363 // done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000365 // If we haven't emitted a token on this line yet, PrevTok isn't
366 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000367 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000368 // Don't print "-" next to "-", it would form "--".
369 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000370 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 }
372
Chris Lattner2933f412007-07-23 06:14:36 +0000373 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner33116d62009-01-26 19:33:54 +0000374 OS.write(II->getName(), II->getLength());
375 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
376 Tok.getLiteralData()) {
377 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner2933f412007-07-23 06:14:36 +0000378 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 const char *TokPtr = Buffer;
380 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000381 OS.write(TokPtr, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 } else {
383 std::string S = PP.getSpelling(Tok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000384 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 }
386 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner6f688e12007-10-10 20:45:16 +0000387
388 if (Tok.is(tok::eof)) break;
Chris Lattner59076ab2009-02-06 05:56:11 +0000389
Chris Lattner6f688e12007-10-10 20:45:16 +0000390 PrevTok = Tok;
391 PP.Lex(Tok);
392 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000393}
394
Chris Lattner2a2bb182009-02-10 22:28:19 +0000395namespace {
396 struct SortMacrosByID {
397 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
398 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
399 return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
400 }
401 };
402}
Chris Lattner59076ab2009-02-06 05:56:11 +0000403
Eli Friedmanf1db5852009-05-19 01:32:34 +0000404void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
405 // -dM mode just scans and ignores all tokens in the files, then dumps out
406 // the macro table at the end.
407 PP.EnterMainSourceFile();
408
409 Token Tok;
410 do PP.Lex(Tok);
411 while (Tok.isNot(tok::eof));
412
413 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
414 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
415 I != E; ++I)
416 MacrosByID.push_back(*I);
417 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
418
419 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
420 MacroInfo &MI = *MacrosByID[i].second;
421 // Ignore computed macros like __LINE__ and friends.
422 if (MI.isBuiltinMacro()) continue;
423
424 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
425 *OS << "\n";
426 }
427}
428
Chris Lattner59076ab2009-02-06 05:56:11 +0000429/// DoPrintPreprocessedInput - This implements -E mode.
430///
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000431void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
432 bool EnableCommentOutput,
433 bool EnableMacroCommentOutput,
434 bool DisableLineMarkers,
435 bool DumpDefines) {
Chris Lattner59076ab2009-02-06 05:56:11 +0000436 // Inform the preprocessor whether we want it to retain comments or not, due
437 // to -C or -CC.
438 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000439
440 OS->SetBufferSize(64*1024);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000441
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000442 PrintPPOutputPPCallbacks *Callbacks =
443 new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000444 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
445 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
446 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000447
Eli Friedmanf1db5852009-05-19 01:32:34 +0000448 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000449
Eli Friedmanf1db5852009-05-19 01:32:34 +0000450 // After we have configured the preprocessor, enter the main file.
451 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000452
Eli Friedmanf1db5852009-05-19 01:32:34 +0000453 // Consume all of the tokens that come from the predefines buffer. Those
454 // should not be emitted into the output and are guaranteed to be at the
455 // start.
456 const SourceManager &SourceMgr = PP.getSourceManager();
457 Token Tok;
458 do PP.Lex(Tok);
459 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
460 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
461 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000462
Eli Friedmanf1db5852009-05-19 01:32:34 +0000463 // Read all the preprocessed tokens, printing them out to the stream.
464 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
465 *OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000466}
467