blob: 89d099caf8a91d148108b180788ec67339e878d8 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Friedman7e1d6372009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Chris Lattner133db8a2009-02-06 06:45:26 +000016#include "clang/Lex/MacroInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Lex/PPCallbacks.h"
18#include "clang/Lex/Preprocessor.h"
19#include "clang/Lex/Pragma.h"
Chris Lattner0eb7cd82009-02-13 00:46:04 +000020#include "clang/Lex/TokenConcatenation.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner6619f662008-04-08 04:16:20 +000022#include "clang/Basic/Diagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/Config/config.h"
Chris Lattner93b4f302008-08-17 01:47:12 +000026#include "llvm/Support/raw_ostream.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027#include <cstdio>
28using namespace clang;
29
Chris Lattner346d9e42009-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());
Jay Foad9e6bef42009-05-21 09:52:38 +000071 const char *Buffer = SpellingBuffer.data();
Chris Lattner346d9e42009-04-12 01:56:53 +000072 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
73 OS.write(Buffer, SpellingLen);
74 }
75}
76
Chris Lattner4b009652007-07-25 00:24:17 +000077//===----------------------------------------------------------------------===//
78// Preprocessed token printer
79//===----------------------------------------------------------------------===//
80
Chris Lattner4b009652007-07-25 00:24:17 +000081namespace {
82class PrintPPOutputPPCallbacks : public PPCallbacks {
83 Preprocessor &PP;
Chris Lattner0eb7cd82009-02-13 00:46:04 +000084 TokenConcatenation ConcatInfo;
Chris Lattner21494222008-08-17 03:12:02 +000085public:
86 llvm::raw_ostream &OS;
87private:
Chris Lattner4b009652007-07-25 00:24:17 +000088 unsigned CurLine;
89 bool EmittedTokensOnThisLine;
Eli Friedmanec0df5f2009-06-02 07:55:39 +000090 bool EmittedMacroOnThisLine;
Chris Lattner7a4864e2008-10-27 01:19:25 +000091 SrcMgr::CharacteristicKind FileType;
Chris Lattner4b009652007-07-25 00:24:17 +000092 llvm::SmallString<512> CurFilename;
Daniel Dunbare0d59462008-09-05 03:22:57 +000093 bool Initialized;
Eli Friedmane2554252009-05-19 03:06:47 +000094 bool DisableLineMarkers;
95 bool DumpDefines;
Chris Lattner4b009652007-07-25 00:24:17 +000096public:
Eli Friedmane2554252009-05-19 03:06:47 +000097 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
98 bool lineMarkers, bool defines)
99 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
100 DumpDefines(defines) {
Chris Lattner4b009652007-07-25 00:24:17 +0000101 CurLine = 0;
102 CurFilename += "<uninit>";
103 EmittedTokensOnThisLine = false;
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000104 EmittedMacroOnThisLine = false;
Chris Lattner6f044062008-09-26 21:18:42 +0000105 FileType = SrcMgr::C_User;
Daniel Dunbare0d59462008-09-05 03:22:57 +0000106 Initialized = false;
Chris Lattner4b009652007-07-25 00:24:17 +0000107 }
108
109 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
110 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
111
112 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000113 SrcMgr::CharacteristicKind FileType);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 virtual void Ident(SourceLocation Loc, const std::string &str);
Chris Lattner183b8392009-01-16 19:25:54 +0000115 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
116 const std::string &Str);
117
Chris Lattner4b009652007-07-25 00:24:17 +0000118
Chris Lattner6c451292007-12-09 21:11:08 +0000119 bool HandleFirstTokOnLine(Token &Tok);
120 bool MoveToLine(SourceLocation Loc);
Chris Lattner0eb7cd82009-02-13 00:46:04 +0000121 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
122 return ConcatInfo.AvoidConcat(PrevTok, Tok);
123 }
Daniel Dunbare0d59462008-09-05 03:22:57 +0000124 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Chris Lattner346d9e42009-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
Chris Lattner4b009652007-07-25 00:24:17 +0000129};
Chris Lattner6619f662008-04-08 04:16:20 +0000130} // end anonymous namespace
Chris Lattner4b009652007-07-25 00:24:17 +0000131
Daniel Dunbare0d59462008-09-05 03:22:57 +0000132void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
133 const char *Extra,
134 unsigned ExtraLen) {
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000135 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
Daniel Dunbare0d59462008-09-05 03:22:57 +0000136 OS << '\n';
137 EmittedTokensOnThisLine = false;
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000138 EmittedMacroOnThisLine = false;
Daniel Dunbare0d59462008-09-05 03:22:57 +0000139 }
140
141 OS << '#' << ' ' << LineNo << ' ' << '"';
142 OS.write(&CurFilename[0], CurFilename.size());
143 OS << '"';
144
145 if (ExtraLen)
146 OS.write(Extra, ExtraLen);
147
Chris Lattner6f044062008-09-26 21:18:42 +0000148 if (FileType == SrcMgr::C_System)
Daniel Dunbare0d59462008-09-05 03:22:57 +0000149 OS.write(" 3", 2);
Chris Lattner6f044062008-09-26 21:18:42 +0000150 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbare0d59462008-09-05 03:22:57 +0000151 OS.write(" 3 4", 4);
152 OS << '\n';
153}
154
Chris Lattner4b009652007-07-25 00:24:17 +0000155/// MoveToLine - Move the output to the source line specified by the location
156/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner6c451292007-12-09 21:11:08 +0000157/// #line directive. This returns false if already at the specified line, true
158/// if some newlines were emitted.
159bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattner18c8dc02009-01-16 07:36:28 +0000160 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbare0d59462008-09-05 03:22:57 +0000161
Chris Lattner4b009652007-07-25 00:24:17 +0000162 if (DisableLineMarkers) {
Chris Lattner6c451292007-12-09 21:11:08 +0000163 if (LineNo == CurLine) return false;
164
165 CurLine = LineNo;
166
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000167 if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine)
Chris Lattner6c451292007-12-09 21:11:08 +0000168 return true;
169
Chris Lattner21494222008-08-17 03:12:02 +0000170 OS << '\n';
Chris Lattner6c451292007-12-09 21:11:08 +0000171 EmittedTokensOnThisLine = false;
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000172 EmittedMacroOnThisLine = false;
Chris Lattner6c451292007-12-09 21:11:08 +0000173 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000174 }
Daniel Dunbare0d59462008-09-05 03:22:57 +0000175
Chris Lattner4b009652007-07-25 00:24:17 +0000176 // If this line is "close enough" to the original line, just print newlines,
177 // otherwise print a #line directive.
Daniel Dunbar6fa81e72008-09-26 01:13:35 +0000178 if (LineNo-CurLine <= 8) {
Chris Lattner4b009652007-07-25 00:24:17 +0000179 if (LineNo-CurLine == 1)
Chris Lattner21494222008-08-17 03:12:02 +0000180 OS << '\n';
Chris Lattner6c451292007-12-09 21:11:08 +0000181 else if (LineNo == CurLine)
Chris Lattner18c8dc02009-01-16 07:36:28 +0000182 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner4b009652007-07-25 00:24:17 +0000183 else {
184 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattner21494222008-08-17 03:12:02 +0000185 OS.write(NewLines, LineNo-CurLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000186 }
187 } else {
Daniel Dunbare0d59462008-09-05 03:22:57 +0000188 WriteLineInfo(LineNo, 0, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000189 }
Daniel Dunbare0d59462008-09-05 03:22:57 +0000190
191 CurLine = LineNo;
Chris Lattner6c451292007-12-09 21:11:08 +0000192 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000193}
194
195
196/// FileChanged - Whenever the preprocessor enters or exits a #include file
197/// it invokes this handler. Update our conception of the current source
198/// position.
199void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
200 FileChangeReason Reason,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000201 SrcMgr::CharacteristicKind NewFileType) {
Chris Lattner4b009652007-07-25 00:24:17 +0000202 // Unless we are exiting a #include, make sure to skip ahead to the line the
203 // #include directive was at.
204 SourceManager &SourceMgr = PP.getSourceManager();
205 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner092fc402009-01-30 18:44:17 +0000206 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
207 if (IncludeLoc.isValid())
208 MoveToLine(IncludeLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000209 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
210 MoveToLine(Loc);
211
212 // TODO GCC emits the # directive for this directive on the line AFTER the
213 // directive and emits a bunch of spaces that aren't needed. Emulate this
214 // strange behavior.
215 }
216
Chris Lattner18c8dc02009-01-16 07:36:28 +0000217 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattnerb79baf52009-03-27 17:13:49 +0000218 // FIXME: Should use presumed line #!
Chris Lattner2d89c562009-02-04 01:06:56 +0000219 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbare0d59462008-09-05 03:22:57 +0000220
Chris Lattner6c451292007-12-09 21:11:08 +0000221 if (DisableLineMarkers) return;
222
Chris Lattner4b009652007-07-25 00:24:17 +0000223 CurFilename.clear();
Chris Lattner836774b2009-01-27 07:57:44 +0000224 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattner4b009652007-07-25 00:24:17 +0000225 Lexer::Stringify(CurFilename);
Daniel Dunbare0d59462008-09-05 03:22:57 +0000226 FileType = NewFileType;
227
228 if (!Initialized) {
229 WriteLineInfo(CurLine);
230 Initialized = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 }
Daniel Dunbare0d59462008-09-05 03:22:57 +0000232
Chris Lattner4b009652007-07-25 00:24:17 +0000233 switch (Reason) {
234 case PPCallbacks::EnterFile:
Daniel Dunbare0d59462008-09-05 03:22:57 +0000235 WriteLineInfo(CurLine, " 1", 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000236 break;
237 case PPCallbacks::ExitFile:
Daniel Dunbare0d59462008-09-05 03:22:57 +0000238 WriteLineInfo(CurLine, " 2", 2);
Chris Lattner4b009652007-07-25 00:24:17 +0000239 break;
Daniel Dunbare0d59462008-09-05 03:22:57 +0000240 case PPCallbacks::SystemHeaderPragma:
241 case PPCallbacks::RenameFile:
242 WriteLineInfo(CurLine);
243 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 }
Chris Lattner4b009652007-07-25 00:24:17 +0000245}
246
Chris Lattner183b8392009-01-16 19:25:54 +0000247/// Ident - Handle #ident directives when read by the preprocessor.
Chris Lattner4b009652007-07-25 00:24:17 +0000248///
249void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
250 MoveToLine(Loc);
251
Chris Lattner21494222008-08-17 03:12:02 +0000252 OS.write("#ident ", strlen("#ident "));
253 OS.write(&S[0], S.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000254 EmittedTokensOnThisLine = true;
255}
256
Chris Lattner346d9e42009-04-12 01:56:53 +0000257/// MacroDefined - This hook is called whenever a macro definition is seen.
258void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
259 const MacroInfo *MI) {
260 // Only print out macro definitions in -dD mode.
261 if (!DumpDefines ||
262 // Ignore __FILE__ etc.
263 MI->isBuiltinMacro()) return;
264
265 MoveToLine(MI->getDefinitionLoc());
266 PrintMacroDefinition(*II, *MI, PP, OS);
Eli Friedmanec0df5f2009-06-02 07:55:39 +0000267 EmittedMacroOnThisLine = true;
Chris Lattner346d9e42009-04-12 01:56:53 +0000268}
269
270
Chris Lattner183b8392009-01-16 19:25:54 +0000271void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
272 const IdentifierInfo *Kind,
273 const std::string &Str) {
274 MoveToLine(Loc);
275 OS << "#pragma comment(" << Kind->getName();
276
277 if (!Str.empty()) {
278 OS << ", \"";
279
280 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
281 unsigned char Char = Str[i];
Chris Lattnere1a7e712009-01-16 22:13:37 +0000282 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattner183b8392009-01-16 19:25:54 +0000283 OS << (char)Char;
284 else // Output anything hard as an octal escape.
285 OS << '\\'
286 << (char)('0'+ ((Char >> 6) & 7))
287 << (char)('0'+ ((Char >> 3) & 7))
288 << (char)('0'+ ((Char >> 0) & 7));
289 }
290 OS << '"';
291 }
292
293 OS << ')';
294 EmittedTokensOnThisLine = true;
295}
296
297
Chris Lattner4b009652007-07-25 00:24:17 +0000298/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner6c451292007-12-09 21:11:08 +0000299/// is called for the first token on each new line. If this really is the start
300/// of a new logical line, handle it and return true, otherwise return false.
301/// This may not be the start of a logical line because the "start of line"
Chris Lattner18c8dc02009-01-16 07:36:28 +0000302/// marker is set for spelling lines, not instantiation ones.
Chris Lattner6c451292007-12-09 21:11:08 +0000303bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +0000304 // Figure out what line we went to and insert the appropriate number of
305 // newline characters.
Chris Lattner6c451292007-12-09 21:11:08 +0000306 if (!MoveToLine(Tok.getLocation()))
307 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000308
309 // Print out space characters so that the first token on a line is
310 // indented for easy reading.
311 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000312 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000313
314 // This hack prevents stuff like:
315 // #define HASH #
316 // HASH define foo bar
317 // From having the # character end up at column 1, which makes it so it
318 // is not handled as a #define next time through the preprocessor if in
319 // -fpreprocessed mode.
Chris Lattner3b494152007-10-09 18:03:42 +0000320 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattner21494222008-08-17 03:12:02 +0000321 OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000322
323 // Otherwise, indent the appropriate number of spaces.
324 for (; ColNo > 1; --ColNo)
Chris Lattner21494222008-08-17 03:12:02 +0000325 OS << ' ';
Chris Lattner6c451292007-12-09 21:11:08 +0000326
327 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000328}
329
330namespace {
331struct UnknownPragmaHandler : public PragmaHandler {
332 const char *Prefix;
333 PrintPPOutputPPCallbacks *Callbacks;
334
335 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
336 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
337 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
338 // Figure out what line we went to and insert the appropriate number of
339 // newline characters.
340 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattner21494222008-08-17 03:12:02 +0000341 Callbacks->OS.write(Prefix, strlen(Prefix));
Chris Lattner4b009652007-07-25 00:24:17 +0000342
343 // Read and print all of the pragma tokens.
Chris Lattner3b494152007-10-09 18:03:42 +0000344 while (PragmaTok.isNot(tok::eom)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000345 if (PragmaTok.hasLeadingSpace())
Chris Lattner21494222008-08-17 03:12:02 +0000346 Callbacks->OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000347 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattner21494222008-08-17 03:12:02 +0000348 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000349 PP.LexUnexpandedToken(PragmaTok);
350 }
Chris Lattner21494222008-08-17 03:12:02 +0000351 Callbacks->OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000352 }
353};
354} // end anonymous namespace
355
356
Chris Lattner163eb372009-02-06 05:56:11 +0000357static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
358 PrintPPOutputPPCallbacks *Callbacks,
359 llvm::raw_ostream &OS) {
Chris Lattner4b009652007-07-25 00:24:17 +0000360 char Buffer[256];
Chris Lattner163eb372009-02-06 05:56:11 +0000361 Token PrevTok;
Chris Lattner3eddc862007-10-10 20:45:16 +0000362 while (1) {
Chris Lattner4b009652007-07-25 00:24:17 +0000363
364 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner6c451292007-12-09 21:11:08 +0000365 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
366 // done.
Chris Lattner4b009652007-07-25 00:24:17 +0000367 } else if (Tok.hasLeadingSpace() ||
368 // If we haven't emitted a token on this line yet, PrevTok isn't
369 // useful to look at and no concatenation could happen anyway.
370 (Callbacks->hasEmittedTokensOnThisLine() &&
371 // Don't print "-" next to "-", it would form "--".
372 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattner21494222008-08-17 03:12:02 +0000373 OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000374 }
375
376 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattnerdb8f9572009-01-26 19:33:54 +0000377 OS.write(II->getName(), II->getLength());
378 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
379 Tok.getLiteralData()) {
380 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner4b009652007-07-25 00:24:17 +0000381 } else if (Tok.getLength() < 256) {
382 const char *TokPtr = Buffer;
383 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattner21494222008-08-17 03:12:02 +0000384 OS.write(TokPtr, Len);
Chris Lattner4b009652007-07-25 00:24:17 +0000385 } else {
386 std::string S = PP.getSpelling(Tok);
Chris Lattner21494222008-08-17 03:12:02 +0000387 OS.write(&S[0], S.size());
Chris Lattner4b009652007-07-25 00:24:17 +0000388 }
389 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner3eddc862007-10-10 20:45:16 +0000390
391 if (Tok.is(tok::eof)) break;
Chris Lattner163eb372009-02-06 05:56:11 +0000392
Chris Lattner3eddc862007-10-10 20:45:16 +0000393 PrevTok = Tok;
394 PP.Lex(Tok);
395 }
Chris Lattner163eb372009-02-06 05:56:11 +0000396}
397
Chris Lattnerf89e7892009-02-10 22:28:19 +0000398namespace {
399 struct SortMacrosByID {
400 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
401 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
402 return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
403 }
404 };
405}
Chris Lattner163eb372009-02-06 05:56:11 +0000406
Eli Friedman50f66262009-05-19 01:32:34 +0000407void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
408 // -dM mode just scans and ignores all tokens in the files, then dumps out
409 // the macro table at the end.
410 PP.EnterMainSourceFile();
411
412 Token Tok;
413 do PP.Lex(Tok);
414 while (Tok.isNot(tok::eof));
415
416 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
417 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
418 I != E; ++I)
419 MacrosByID.push_back(*I);
420 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
421
422 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
423 MacroInfo &MI = *MacrosByID[i].second;
424 // Ignore computed macros like __LINE__ and friends.
425 if (MI.isBuiltinMacro()) continue;
426
427 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
428 *OS << "\n";
429 }
430}
431
Chris Lattner163eb372009-02-06 05:56:11 +0000432/// DoPrintPreprocessedInput - This implements -E mode.
433///
Eli Friedmane2554252009-05-19 03:06:47 +0000434void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
435 bool EnableCommentOutput,
436 bool EnableMacroCommentOutput,
437 bool DisableLineMarkers,
438 bool DumpDefines) {
Chris Lattner163eb372009-02-06 05:56:11 +0000439 // Inform the preprocessor whether we want it to retain comments or not, due
440 // to -C or -CC.
441 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Eli Friedman4ae15b92009-05-19 01:02:07 +0000442
443 OS->SetBufferSize(64*1024);
Chris Lattner346d9e42009-04-12 01:56:53 +0000444
Eli Friedmane2554252009-05-19 03:06:47 +0000445 PrintPPOutputPPCallbacks *Callbacks =
446 new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines);
Eli Friedman50f66262009-05-19 01:32:34 +0000447 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
448 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
449 Callbacks));
Chris Lattner163eb372009-02-06 05:56:11 +0000450
Eli Friedman50f66262009-05-19 01:32:34 +0000451 PP.setPPCallbacks(Callbacks);
Chris Lattner163eb372009-02-06 05:56:11 +0000452
Eli Friedman50f66262009-05-19 01:32:34 +0000453 // After we have configured the preprocessor, enter the main file.
454 PP.EnterMainSourceFile();
Chris Lattner163eb372009-02-06 05:56:11 +0000455
Eli Friedman50f66262009-05-19 01:32:34 +0000456 // Consume all of the tokens that come from the predefines buffer. Those
457 // should not be emitted into the output and are guaranteed to be at the
458 // start.
459 const SourceManager &SourceMgr = PP.getSourceManager();
460 Token Tok;
461 do PP.Lex(Tok);
462 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
463 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
464 "<built-in>"));
Chris Lattner163eb372009-02-06 05:56:11 +0000465
Eli Friedman50f66262009-05-19 01:32:34 +0000466 // Read all the preprocessed tokens, printing them out to the stream.
467 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
468 *OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000469}
470