blob: d88e57d85dfd22818a32d7af63103fba5d56b401 [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"
25#include "llvm/Config/config.h"
Chris Lattnerdceb6a72008-08-17 01:47:12 +000026#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include <cstdio>
28using namespace clang;
29
Chris Lattnerd82df3a2009-04-12 01:56:53 +000030/// PrintMacroDefinition - Print a macro definition in a form that will be
31/// properly accepted back as a definition.
32static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
33 Preprocessor &PP, llvm::raw_ostream &OS) {
34 OS << "#define " << II.getName();
35
36 if (MI.isFunctionLike()) {
37 OS << '(';
38 if (MI.arg_empty())
39 ;
40 else if (MI.getNumArgs() == 1)
41 OS << (*MI.arg_begin())->getName();
42 else {
43 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
44 OS << (*AI++)->getName();
45 while (AI != E)
46 OS << ',' << (*AI++)->getName();
47 }
48
49 if (MI.isVariadic()) {
50 if (!MI.arg_empty())
51 OS << ',';
52 OS << "...";
53 }
54 OS << ')';
55 }
56
57 // GCC always emits a space, even if the macro body is empty. However, do not
58 // want to emit two spaces if the first token has a leading space.
59 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
60 OS << ' ';
61
62 llvm::SmallVector<char, 128> SpellingBuffer;
63 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
64 I != E; ++I) {
65 if (I->hasLeadingSpace())
66 OS << ' ';
67
68 // Make sure we have enough space in the spelling buffer.
69 if (I->getLength() < SpellingBuffer.size())
70 SpellingBuffer.resize(I->getLength());
71 const char *Buffer = &SpellingBuffer[0];
72 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
73 OS.write(Buffer, SpellingLen);
74 }
75}
76
Reid Spencer5f016e22007-07-11 17:01:13 +000077//===----------------------------------------------------------------------===//
78// Preprocessed token printer
79//===----------------------------------------------------------------------===//
80
Reid Spencer5f016e22007-07-11 17:01:13 +000081namespace {
82class PrintPPOutputPPCallbacks : public PPCallbacks {
83 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +000084 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +000085public:
86 llvm::raw_ostream &OS;
87private:
Reid Spencer5f016e22007-07-11 17:01:13 +000088 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +000089 bool EmittedTokensOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +000090 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +000091 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +000092 bool Initialized;
Eli Friedman12d3b1d2009-05-19 03:06:47 +000093 bool DisableLineMarkers;
94 bool DumpDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +000095public:
Eli Friedman12d3b1d2009-05-19 03:06:47 +000096 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
97 bool lineMarkers, bool defines)
98 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
99 DumpDefines(defines) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000101 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 EmittedTokensOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000103 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000104 Initialized = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 }
106
107 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000108 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000109
110 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000111 SrcMgr::CharacteristicKind FileType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 virtual void Ident(SourceLocation Loc, const std::string &str);
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000113 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
114 const std::string &Str);
115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
Chris Lattner5f180322007-12-09 21:11:08 +0000117 bool HandleFirstTokOnLine(Token &Tok);
118 bool MoveToLine(SourceLocation Loc);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000119 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
120 return ConcatInfo.AvoidConcat(PrevTok, Tok);
121 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000122 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000123
124 /// MacroDefined - This hook is called whenever a macro definition is seen.
125 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127};
Chris Lattner5db17c92008-04-08 04:16:20 +0000128} // end anonymous namespace
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000130void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
131 const char *Extra,
132 unsigned ExtraLen) {
133 if (EmittedTokensOnThisLine) {
134 OS << '\n';
135 EmittedTokensOnThisLine = false;
136 }
137
138 OS << '#' << ' ' << LineNo << ' ' << '"';
139 OS.write(&CurFilename[0], CurFilename.size());
140 OS << '"';
141
142 if (ExtraLen)
143 OS.write(Extra, ExtraLen);
144
Chris Lattner0b9e7362008-09-26 21:18:42 +0000145 if (FileType == SrcMgr::C_System)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000146 OS.write(" 3", 2);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000147 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000148 OS.write(" 3 4", 4);
149 OS << '\n';
150}
151
Reid Spencer5f016e22007-07-11 17:01:13 +0000152/// MoveToLine - Move the output to the source line specified by the location
153/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner5f180322007-12-09 21:11:08 +0000154/// #line directive. This returns false if already at the specified line, true
155/// if some newlines were emitted.
156bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000157 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000160 if (LineNo == CurLine) return false;
161
162 CurLine = LineNo;
163
164 if (!EmittedTokensOnThisLine)
165 return true;
166
Chris Lattnere96de3e2008-08-17 03:12:02 +0000167 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000168 EmittedTokensOnThisLine = false;
169 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // If this line is "close enough" to the original line, just print newlines,
173 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000174 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000175 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000176 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000177 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000178 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000179 else {
180 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000181 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000182 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000184 WriteLineInfo(LineNo, 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000186
187 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000188 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000189}
190
191
192/// FileChanged - Whenever the preprocessor enters or exits a #include file
193/// it invokes this handler. Update our conception of the current source
194/// position.
195void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
196 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000197 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 // Unless we are exiting a #include, make sure to skip ahead to the line the
199 // #include directive was at.
200 SourceManager &SourceMgr = PP.getSourceManager();
201 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000202 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
203 if (IncludeLoc.isValid())
204 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
206 MoveToLine(Loc);
207
208 // TODO GCC emits the # directive for this directive on the line AFTER the
209 // directive and emits a bunch of spaces that aren't needed. Emulate this
210 // strange behavior.
211 }
212
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000213 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000214 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000215 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000216
Chris Lattner5f180322007-12-09 21:11:08 +0000217 if (DisableLineMarkers) return;
218
Chris Lattnerd8e30832007-07-24 06:57:14 +0000219 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000220 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000221 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000222 FileType = NewFileType;
223
224 if (!Initialized) {
225 WriteLineInfo(CurLine);
226 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000227 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 switch (Reason) {
230 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000231 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 break;
233 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000234 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 break;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000236 case PPCallbacks::SystemHeaderPragma:
237 case PPCallbacks::RenameFile:
238 WriteLineInfo(CurLine);
239 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000241}
242
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000243/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000244///
245void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
246 MoveToLine(Loc);
247
Chris Lattnere96de3e2008-08-17 03:12:02 +0000248 OS.write("#ident ", strlen("#ident "));
249 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 EmittedTokensOnThisLine = true;
251}
252
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000253/// MacroDefined - This hook is called whenever a macro definition is seen.
254void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
255 const MacroInfo *MI) {
256 // Only print out macro definitions in -dD mode.
257 if (!DumpDefines ||
258 // Ignore __FILE__ etc.
259 MI->isBuiltinMacro()) return;
260
261 MoveToLine(MI->getDefinitionLoc());
262 PrintMacroDefinition(*II, *MI, PP, OS);
263}
264
265
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000266void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
267 const IdentifierInfo *Kind,
268 const std::string &Str) {
269 MoveToLine(Loc);
270 OS << "#pragma comment(" << Kind->getName();
271
272 if (!Str.empty()) {
273 OS << ", \"";
274
275 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
276 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000277 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000278 OS << (char)Char;
279 else // Output anything hard as an octal escape.
280 OS << '\\'
281 << (char)('0'+ ((Char >> 6) & 7))
282 << (char)('0'+ ((Char >> 3) & 7))
283 << (char)('0'+ ((Char >> 0) & 7));
284 }
285 OS << '"';
286 }
287
288 OS << ')';
289 EmittedTokensOnThisLine = true;
290}
291
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000294/// is called for the first token on each new line. If this really is the start
295/// of a new logical line, handle it and return true, otherwise return false.
296/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000297/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000298bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 // Figure out what line we went to and insert the appropriate number of
300 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000301 if (!MoveToLine(Tok.getLocation()))
302 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000303
304 // Print out space characters so that the first token on a line is
305 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000306 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000307 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000308
309 // This hack prevents stuff like:
310 // #define HASH #
311 // HASH define foo bar
312 // From having the # character end up at column 1, which makes it so it
313 // is not handled as a #define next time through the preprocessor if in
314 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000315 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000316 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000317
318 // Otherwise, indent the appropriate number of spaces.
319 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000320 OS << ' ';
Chris Lattner5f180322007-12-09 21:11:08 +0000321
322 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000323}
324
325namespace {
326struct UnknownPragmaHandler : public PragmaHandler {
327 const char *Prefix;
328 PrintPPOutputPPCallbacks *Callbacks;
329
330 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
331 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000332 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 // Figure out what line we went to and insert the appropriate number of
334 // newline characters.
335 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000336 Callbacks->OS.write(Prefix, strlen(Prefix));
Reid Spencer5f016e22007-07-11 17:01:13 +0000337
338 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000339 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000341 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000343 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 PP.LexUnexpandedToken(PragmaTok);
345 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000346 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 }
348};
349} // end anonymous namespace
350
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000351
Chris Lattner59076ab2009-02-06 05:56:11 +0000352static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
353 PrintPPOutputPPCallbacks *Callbacks,
354 llvm::raw_ostream &OS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000356 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000357 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358
359 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000360 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
361 // done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000363 // If we haven't emitted a token on this line yet, PrevTok isn't
364 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000365 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000366 // Don't print "-" next to "-", it would form "--".
367 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000368 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
370
Chris Lattner2933f412007-07-23 06:14:36 +0000371 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner33116d62009-01-26 19:33:54 +0000372 OS.write(II->getName(), II->getLength());
373 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
374 Tok.getLiteralData()) {
375 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner2933f412007-07-23 06:14:36 +0000376 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000377 const char *TokPtr = Buffer;
378 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000379 OS.write(TokPtr, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 } else {
381 std::string S = PP.getSpelling(Tok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000382 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 }
384 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner6f688e12007-10-10 20:45:16 +0000385
386 if (Tok.is(tok::eof)) break;
Chris Lattner59076ab2009-02-06 05:56:11 +0000387
Chris Lattner6f688e12007-10-10 20:45:16 +0000388 PrevTok = Tok;
389 PP.Lex(Tok);
390 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000391}
392
Chris Lattner2a2bb182009-02-10 22:28:19 +0000393namespace {
394 struct SortMacrosByID {
395 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
396 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
397 return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
398 }
399 };
400}
Chris Lattner59076ab2009-02-06 05:56:11 +0000401
Eli Friedmanf1db5852009-05-19 01:32:34 +0000402void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
403 // -dM mode just scans and ignores all tokens in the files, then dumps out
404 // the macro table at the end.
405 PP.EnterMainSourceFile();
406
407 Token Tok;
408 do PP.Lex(Tok);
409 while (Tok.isNot(tok::eof));
410
411 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
412 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
413 I != E; ++I)
414 MacrosByID.push_back(*I);
415 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
416
417 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
418 MacroInfo &MI = *MacrosByID[i].second;
419 // Ignore computed macros like __LINE__ and friends.
420 if (MI.isBuiltinMacro()) continue;
421
422 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
423 *OS << "\n";
424 }
425}
426
Chris Lattner59076ab2009-02-06 05:56:11 +0000427/// DoPrintPreprocessedInput - This implements -E mode.
428///
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000429void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
430 bool EnableCommentOutput,
431 bool EnableMacroCommentOutput,
432 bool DisableLineMarkers,
433 bool DumpDefines) {
Chris Lattner59076ab2009-02-06 05:56:11 +0000434 // Inform the preprocessor whether we want it to retain comments or not, due
435 // to -C or -CC.
436 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000437
438 OS->SetBufferSize(64*1024);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000439
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000440 PrintPPOutputPPCallbacks *Callbacks =
441 new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000442 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
443 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
444 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000445
Eli Friedmanf1db5852009-05-19 01:32:34 +0000446 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000447
Eli Friedmanf1db5852009-05-19 01:32:34 +0000448 // After we have configured the preprocessor, enter the main file.
449 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000450
Eli Friedmanf1db5852009-05-19 01:32:34 +0000451 // Consume all of the tokens that come from the predefines buffer. Those
452 // should not be emitted into the output and are guaranteed to be at the
453 // start.
454 const SourceManager &SourceMgr = PP.getSourceManager();
455 Token Tok;
456 do PP.Lex(Tok);
457 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
458 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
459 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000460
Eli Friedmanf1db5852009-05-19 01:32:34 +0000461 // Read all the preprocessed tokens, printing them out to the stream.
462 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
463 *OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465