blob: 429b8296490f00f2b0dc66a918ef96a2c4252c0a [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
Reid Spencer5f016e22007-07-11 17:01:13 +000084namespace {
85class PrintPPOutputPPCallbacks : public PPCallbacks {
86 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +000087 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +000088public:
89 llvm::raw_ostream &OS;
90private:
Reid Spencer5f016e22007-07-11 17:01:13 +000091 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +000092 bool EmittedTokensOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +000093 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +000094 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +000095 bool Initialized;
Eli Friedman12d3b1d2009-05-19 03:06:47 +000096 bool DisableLineMarkers;
97 bool DumpDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +000098public:
Eli Friedman12d3b1d2009-05-19 03:06:47 +000099 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
100 bool lineMarkers, bool defines)
101 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
102 DumpDefines(defines) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000104 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 EmittedTokensOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000106 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000107 Initialized = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
109
110 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000111 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Reid Spencer5f016e22007-07-11 17:01:13 +0000112
113 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000114 SrcMgr::CharacteristicKind FileType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 virtual void Ident(SourceLocation Loc, const std::string &str);
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000116 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
117 const std::string &Str);
118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
Chris Lattner5f180322007-12-09 21:11:08 +0000120 bool HandleFirstTokOnLine(Token &Tok);
121 bool MoveToLine(SourceLocation Loc);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000122 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
123 return ConcatInfo.AvoidConcat(PrevTok, Tok);
124 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000125 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000126
127 /// MacroDefined - This hook is called whenever a macro definition is seen.
128 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130};
Chris Lattner5db17c92008-04-08 04:16:20 +0000131} // end anonymous namespace
Reid Spencer5f016e22007-07-11 17:01:13 +0000132
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000133void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
134 const char *Extra,
135 unsigned ExtraLen) {
136 if (EmittedTokensOnThisLine) {
137 OS << '\n';
138 EmittedTokensOnThisLine = false;
139 }
140
141 OS << '#' << ' ' << LineNo << ' ' << '"';
142 OS.write(&CurFilename[0], CurFilename.size());
143 OS << '"';
144
145 if (ExtraLen)
146 OS.write(Extra, ExtraLen);
147
Chris Lattner0b9e7362008-09-26 21:18:42 +0000148 if (FileType == SrcMgr::C_System)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000149 OS.write(" 3", 2);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000150 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000151 OS.write(" 3 4", 4);
152 OS << '\n';
153}
154
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner5f180322007-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 Lattnerf7cf85b2009-01-16 07:36:28 +0000160 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000161
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000163 if (LineNo == CurLine) return false;
164
165 CurLine = LineNo;
166
167 if (!EmittedTokensOnThisLine)
168 return true;
169
Chris Lattnere96de3e2008-08-17 03:12:02 +0000170 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000171 EmittedTokensOnThisLine = false;
172 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000174
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 // If this line is "close enough" to the original line, just print newlines,
176 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000177 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000178 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000179 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000180 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000181 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000182 else {
183 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000184 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000185 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000187 WriteLineInfo(LineNo, 0, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000189
190 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000191 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000192}
193
194
195/// FileChanged - Whenever the preprocessor enters or exits a #include file
196/// it invokes this handler. Update our conception of the current source
197/// position.
198void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
199 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000200 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 // Unless we are exiting a #include, make sure to skip ahead to the line the
202 // #include directive was at.
203 SourceManager &SourceMgr = PP.getSourceManager();
204 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000205 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
206 if (IncludeLoc.isValid())
207 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
209 MoveToLine(Loc);
210
211 // TODO GCC emits the # directive for this directive on the line AFTER the
212 // directive and emits a bunch of spaces that aren't needed. Emulate this
213 // strange behavior.
214 }
215
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000216 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000217 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000218 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000219
Chris Lattner5f180322007-12-09 21:11:08 +0000220 if (DisableLineMarkers) return;
221
Chris Lattnerd8e30832007-07-24 06:57:14 +0000222 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000223 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000224 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000225 FileType = NewFileType;
226
227 if (!Initialized) {
228 WriteLineInfo(CurLine);
229 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 switch (Reason) {
233 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000234 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 break;
236 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000237 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 break;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000239 case PPCallbacks::SystemHeaderPragma:
240 case PPCallbacks::RenameFile:
241 WriteLineInfo(CurLine);
242 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000246/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000247///
248void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
249 MoveToLine(Loc);
250
Chris Lattnere96de3e2008-08-17 03:12:02 +0000251 OS.write("#ident ", strlen("#ident "));
252 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 EmittedTokensOnThisLine = true;
254}
255
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000256/// MacroDefined - This hook is called whenever a macro definition is seen.
257void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
258 const MacroInfo *MI) {
259 // Only print out macro definitions in -dD mode.
260 if (!DumpDefines ||
261 // Ignore __FILE__ etc.
262 MI->isBuiltinMacro()) return;
263
264 MoveToLine(MI->getDefinitionLoc());
265 PrintMacroDefinition(*II, *MI, PP, OS);
266}
267
268
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000269void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
270 const IdentifierInfo *Kind,
271 const std::string &Str) {
272 MoveToLine(Loc);
273 OS << "#pragma comment(" << Kind->getName();
274
275 if (!Str.empty()) {
276 OS << ", \"";
277
278 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
279 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000280 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000281 OS << (char)Char;
282 else // Output anything hard as an octal escape.
283 OS << '\\'
284 << (char)('0'+ ((Char >> 6) & 7))
285 << (char)('0'+ ((Char >> 3) & 7))
286 << (char)('0'+ ((Char >> 0) & 7));
287 }
288 OS << '"';
289 }
290
291 OS << ')';
292 EmittedTokensOnThisLine = true;
293}
294
295
Reid Spencer5f016e22007-07-11 17:01:13 +0000296/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000297/// is called for the first token on each new line. If this really is the start
298/// of a new logical line, handle it and return true, otherwise return false.
299/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000300/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000301bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 // Figure out what line we went to and insert the appropriate number of
303 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000304 if (!MoveToLine(Tok.getLocation()))
305 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306
307 // Print out space characters so that the first token on a line is
308 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000309 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000310 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000311
312 // This hack prevents stuff like:
313 // #define HASH #
314 // HASH define foo bar
315 // From having the # character end up at column 1, which makes it so it
316 // is not handled as a #define next time through the preprocessor if in
317 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000318 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000319 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000320
321 // Otherwise, indent the appropriate number of spaces.
322 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000323 OS << ' ';
Chris Lattner5f180322007-12-09 21:11:08 +0000324
325 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
328namespace {
329struct UnknownPragmaHandler : public PragmaHandler {
330 const char *Prefix;
331 PrintPPOutputPPCallbacks *Callbacks;
332
333 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
334 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000335 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 // Figure out what line we went to and insert the appropriate number of
337 // newline characters.
338 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000339 Callbacks->OS.write(Prefix, strlen(Prefix));
Reid Spencer5f016e22007-07-11 17:01:13 +0000340
341 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000342 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000344 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000346 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 PP.LexUnexpandedToken(PragmaTok);
348 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000349 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
351};
352} // end anonymous namespace
353
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000354
Chris Lattner59076ab2009-02-06 05:56:11 +0000355static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
356 PrintPPOutputPPCallbacks *Callbacks,
357 llvm::raw_ostream &OS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000359 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000360 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000361
362 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000363 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
364 // done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000366 // If we haven't emitted a token on this line yet, PrevTok isn't
367 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000368 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000369 // Don't print "-" next to "-", it would form "--".
370 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000371 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000372 }
373
Chris Lattner2933f412007-07-23 06:14:36 +0000374 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Chris Lattner33116d62009-01-26 19:33:54 +0000375 OS.write(II->getName(), II->getLength());
376 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
377 Tok.getLiteralData()) {
378 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner2933f412007-07-23 06:14:36 +0000379 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 const char *TokPtr = Buffer;
381 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000382 OS.write(TokPtr, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 } else {
384 std::string S = PP.getSpelling(Tok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000385 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 }
387 Callbacks->SetEmittedTokensOnThisLine();
Chris Lattner6f688e12007-10-10 20:45:16 +0000388
389 if (Tok.is(tok::eof)) break;
Chris Lattner59076ab2009-02-06 05:56:11 +0000390
Chris Lattner6f688e12007-10-10 20:45:16 +0000391 PrevTok = Tok;
392 PP.Lex(Tok);
393 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000394}
395
Chris Lattner2a2bb182009-02-10 22:28:19 +0000396namespace {
397 struct SortMacrosByID {
398 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
399 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
400 return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
401 }
402 };
403}
Chris Lattner59076ab2009-02-06 05:56:11 +0000404
Eli Friedmanf1db5852009-05-19 01:32:34 +0000405void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
406 // -dM mode just scans and ignores all tokens in the files, then dumps out
407 // the macro table at the end.
408 PP.EnterMainSourceFile();
409
410 Token Tok;
411 do PP.Lex(Tok);
412 while (Tok.isNot(tok::eof));
413
414 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
415 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
416 I != E; ++I)
417 MacrosByID.push_back(*I);
418 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
419
420 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
421 MacroInfo &MI = *MacrosByID[i].second;
422 // Ignore computed macros like __LINE__ and friends.
423 if (MI.isBuiltinMacro()) continue;
424
425 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
426 *OS << "\n";
427 }
428}
429
Chris Lattner59076ab2009-02-06 05:56:11 +0000430/// DoPrintPreprocessedInput - This implements -E mode.
431///
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000432void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
433 bool EnableCommentOutput,
434 bool EnableMacroCommentOutput,
435 bool DisableLineMarkers,
436 bool DumpDefines) {
Chris Lattner59076ab2009-02-06 05:56:11 +0000437 // Inform the preprocessor whether we want it to retain comments or not, due
438 // to -C or -CC.
439 PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000440
441 OS->SetBufferSize(64*1024);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000442
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000443 PrintPPOutputPPCallbacks *Callbacks =
444 new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000445 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
446 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
447 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000448
Eli Friedmanf1db5852009-05-19 01:32:34 +0000449 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000450
Eli Friedmanf1db5852009-05-19 01:32:34 +0000451 // After we have configured the preprocessor, enter the main file.
452 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000453
Eli Friedmanf1db5852009-05-19 01:32:34 +0000454 // Consume all of the tokens that come from the predefines buffer. Those
455 // should not be emitted into the output and are guaranteed to be at the
456 // start.
457 const SourceManager &SourceMgr = PP.getSourceManager();
458 Token Tok;
459 do PP.Lex(Tok);
460 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
461 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
462 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000463
Eli Friedmanf1db5852009-05-19 01:32:34 +0000464 // Read all the preprocessed tokens, printing them out to the stream.
465 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
466 *OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000467}
468