blob: 61f2b9e288420a976448225fc37714afa02dc197 [file] [log] [blame]
Chris Lattner09e3cdf2006-07-04 19:04:05 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner09e3cdf2006-07-04 19:04:05 +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 Friedman16b7b6f2009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000016#include "clang/Basic/CharInfo.h"
Daniel Dunbar531f6c62009-11-11 10:07:22 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Frontend/PreprocessorOutputOptions.h"
Chris Lattner1630c3c2009-02-06 06:45:26 +000020#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +000021#include "clang/Lex/PPCallbacks.h"
Chris Lattner09e3cdf2006-07-04 19:04:05 +000022#include "clang/Lex/Pragma.h"
Daniel Dunbar531f6c62009-11-11 10:07:22 +000023#include "clang/Lex/Preprocessor.h"
Chris Lattner644d4522009-02-13 00:46:04 +000024#include "clang/Lex/TokenConcatenation.h"
Benjamin Kramerfb5f40f2010-01-19 17:42:20 +000025#include "llvm/ADT/STLExtras.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner30c924b2010-06-26 17:11:39 +000027#include "llvm/ADT/StringRef.h"
Douglas Gregor3bde9b12011-06-22 19:41:48 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000030#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000031using namespace clang;
32
Chris Lattnercac63f32009-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,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000036 Preprocessor &PP, raw_ostream &OS) {
Chris Lattnercac63f32009-04-12 01:56:53 +000037 OS << "#define " << II.getName();
Mike Stump11289f42009-09-09 15:08:12 +000038
Chris Lattnercac63f32009-04-12 01:56:53 +000039 if (MI.isFunctionLike()) {
40 OS << '(';
Chris Lattner53d80e22009-12-09 02:08:14 +000041 if (!MI.arg_empty()) {
Chris Lattnercac63f32009-04-12 01:56:53 +000042 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
Chris Lattner53d80e22009-12-09 02:08:14 +000043 for (; AI+1 != E; ++AI) {
44 OS << (*AI)->getName();
Chris Lattner9dfed9f2009-12-07 01:58:34 +000045 OS << ',';
Chris Lattner9dfed9f2009-12-07 01:58:34 +000046 }
Chris Lattner53d80e22009-12-09 02:08:14 +000047
48 // Last argument.
49 if ((*AI)->getName() == "__VA_ARGS__")
50 OS << "...";
51 else
52 OS << (*AI)->getName();
Chris Lattnercac63f32009-04-12 01:56:53 +000053 }
Mike Stump11289f42009-09-09 15:08:12 +000054
Chris Lattner9dfed9f2009-12-07 01:58:34 +000055 if (MI.isGNUVarargs())
56 OS << "..."; // #define foo(x...)
Kovarththanan Rajaratnam752a1242010-03-07 07:30:06 +000057
Chris Lattnercac63f32009-04-12 01:56:53 +000058 OS << ')';
59 }
Mike Stump11289f42009-09-09 15:08:12 +000060
Chris Lattnercac63f32009-04-12 01:56:53 +000061 // GCC always emits a space, even if the macro body is empty. However, do not
62 // want to emit two spaces if the first token has a leading space.
63 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
64 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +000065
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000066 SmallString<128> SpellingBuffer;
Chris Lattnercac63f32009-04-12 01:56:53 +000067 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
68 I != E; ++I) {
69 if (I->hasLeadingSpace())
70 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +000071
Benjamin Kramer0a1abd42010-02-27 13:44:12 +000072 OS << PP.getSpelling(*I, SpellingBuffer);
Chris Lattnercac63f32009-04-12 01:56:53 +000073 }
74}
75
Chris Lattnerf46be6c2006-07-04 22:19:33 +000076//===----------------------------------------------------------------------===//
77// Preprocessed token printer
78//===----------------------------------------------------------------------===//
79
Chris Lattner87f267e2006-11-21 05:02:33 +000080namespace {
81class PrintPPOutputPPCallbacks : public PPCallbacks {
82 Preprocessor &PP;
Chris Lattner9d94f042010-04-13 00:06:42 +000083 SourceManager &SM;
Chris Lattner644d4522009-02-13 00:46:04 +000084 TokenConcatenation ConcatInfo;
Chris Lattner068529a2008-08-17 03:12:02 +000085public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +000086 raw_ostream &OS;
Chris Lattner068529a2008-08-17 03:12:02 +000087private:
Chris Lattner87f267e2006-11-21 05:02:33 +000088 unsigned CurLine;
Daniel Dunbard4352752010-08-24 22:44:13 +000089
Chris Lattner87f267e2006-11-21 05:02:33 +000090 bool EmittedTokensOnThisLine;
Jordan Rose127f6ee2012-06-15 23:33:51 +000091 bool EmittedDirectiveOnThisLine;
Chris Lattner66a740e2008-10-27 01:19:25 +000092 SrcMgr::CharacteristicKind FileType;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000093 SmallString<512> CurFilename;
Daniel Dunbar98e0e532008-09-05 03:22:57 +000094 bool Initialized;
Eli Friedman6af494b2009-05-19 03:06:47 +000095 bool DisableLineMarkers;
96 bool DumpDefines;
Chris Lattner76b44452009-12-07 01:42:56 +000097 bool UseLineDirective;
Daniel Dunbar5d388752012-11-16 01:51:11 +000098 bool IsFirstFileEntered;
Chris Lattner87f267e2006-11-21 05:02:33 +000099public:
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000100 PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000101 bool lineMarkers, bool defines)
Chris Lattner9d94f042010-04-13 00:06:42 +0000102 : PP(pp), SM(PP.getSourceManager()),
103 ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000104 DumpDefines(defines) {
105 CurLine = 0;
Chris Lattner4c4a2452007-07-24 06:57:14 +0000106 CurFilename += "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000107 EmittedTokensOnThisLine = false;
Jordan Rose127f6ee2012-06-15 23:33:51 +0000108 EmittedDirectiveOnThisLine = false;
Chris Lattnerb03dc762008-09-26 21:18:42 +0000109 FileType = SrcMgr::C_User;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000110 Initialized = false;
Daniel Dunbar5d388752012-11-16 01:51:11 +0000111 IsFirstFileEntered = false;
Kovarththanan Rajaratnam752a1242010-03-07 07:30:06 +0000112
Chris Lattner76b44452009-12-07 01:42:56 +0000113 // If we're in microsoft mode, use normal #line instead of line markers.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000114 UseLineDirective = PP.getLangOpts().MicrosoftExt;
Chris Lattner87f267e2006-11-21 05:02:33 +0000115 }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Jordan Rose127f6ee2012-06-15 23:33:51 +0000117 void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000118 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Jordan Rose127f6ee2012-06-15 23:33:51 +0000120 void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
121 bool hasEmittedDirectiveOnThisLine() const {
122 return EmittedDirectiveOnThisLine;
123 }
124
125 bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
Douglas Gregorc7d65762010-09-09 22:45:38 +0000126
Chris Lattner87f267e2006-11-21 05:02:33 +0000127 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000128 SrcMgr::CharacteristicKind FileType,
129 FileID PrevFID);
Argyrios Kyrtzidisa6444b12013-04-10 01:53:46 +0000130 virtual void InclusionDirective(SourceLocation HashLoc,
131 const Token &IncludeTok,
132 StringRef FileName,
133 bool IsAngled,
134 CharSourceRange FilenameRange,
135 const FileEntry *File,
136 StringRef SearchPath,
137 StringRef RelativePath,
138 const Module *Imported);
Chris Lattner87f267e2006-11-21 05:02:33 +0000139 virtual void Ident(SourceLocation Loc, const std::string &str);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000140 virtual void PragmaCaptured(SourceLocation Loc, StringRef Str);
Mike Stump11289f42009-09-09 15:08:12 +0000141 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000142 const std::string &Str);
Andy Gibbs9c2ccd62013-04-17 16:16:16 +0000143 virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
144 PragmaMessageKind Kind, StringRef Str);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000145 virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000146 virtual void PragmaDiagnosticPush(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000147 StringRef Namespace);
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000148 virtual void PragmaDiagnosticPop(SourceLocation Loc,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000149 StringRef Namespace);
150 virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000151 diag::Mapping Map, StringRef Str);
Chris Lattner87f267e2006-11-21 05:02:33 +0000152
Chris Lattner3ed83c12007-12-09 21:11:08 +0000153 bool HandleFirstTokOnLine(Token &Tok);
Hal Finkel2109f232013-01-28 04:37:37 +0000154
155 /// Move to the line of the provided source location. This will
156 /// return true if the output stream required adjustment or if
157 /// the requested location is on the first line.
Chris Lattner5dbefc62010-04-13 00:01:41 +0000158 bool MoveToLine(SourceLocation Loc) {
Douglas Gregor453b0122010-11-12 07:15:47 +0000159 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
160 if (PLoc.isInvalid())
161 return false;
Hal Finkel2109f232013-01-28 04:37:37 +0000162 return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
Chris Lattner5dbefc62010-04-13 00:01:41 +0000163 }
164 bool MoveToLine(unsigned LineNo);
165
Chris Lattner0384e6352010-04-14 03:57:19 +0000166 bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
167 const Token &Tok) {
168 return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
Chris Lattner644d4522009-02-13 00:46:04 +0000169 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000170 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Douglas Gregorc7d65762010-09-09 22:45:38 +0000171 bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000172 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattnercac63f32009-04-12 01:56:53 +0000174 /// MacroDefined - This hook is called whenever a macro definition is seen.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000175 void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD);
Mike Stump11289f42009-09-09 15:08:12 +0000176
Benjamin Kramerd05f31d2010-08-07 22:27:00 +0000177 /// MacroUndefined - This hook is called whenever a macro #undef is seen.
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000178 void MacroUndefined(const Token &MacroNameTok, const MacroDirective *MD);
Chris Lattner87f267e2006-11-21 05:02:33 +0000179};
Chris Lattner21632652008-04-08 04:16:20 +0000180} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000181
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000182void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
183 const char *Extra,
184 unsigned ExtraLen) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000185 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000186
Chris Lattner76b44452009-12-07 01:42:56 +0000187 // Emit #line directives or GNU line markers depending on what mode we're in.
188 if (UseLineDirective) {
189 OS << "#line" << ' ' << LineNo << ' ' << '"';
Ted Kremenek6174ca42010-09-17 00:41:18 +0000190 OS.write(CurFilename.data(), CurFilename.size());
Chris Lattner76b44452009-12-07 01:42:56 +0000191 OS << '"';
192 } else {
193 OS << '#' << ' ' << LineNo << ' ' << '"';
Ted Kremenek6174ca42010-09-17 00:41:18 +0000194 OS.write(CurFilename.data(), CurFilename.size());
Chris Lattner76b44452009-12-07 01:42:56 +0000195 OS << '"';
Kovarththanan Rajaratnam752a1242010-03-07 07:30:06 +0000196
Steve Naroff66aaa392009-12-06 01:02:14 +0000197 if (ExtraLen)
198 OS.write(Extra, ExtraLen);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000199
Steve Naroff66aaa392009-12-06 01:02:14 +0000200 if (FileType == SrcMgr::C_System)
201 OS.write(" 3", 2);
202 else if (FileType == SrcMgr::C_ExternCSystem)
203 OS.write(" 3 4", 4);
204 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000205 OS << '\n';
206}
207
Chris Lattner728b4dc2006-07-04 21:28:37 +0000208/// MoveToLine - Move the output to the source line specified by the location
209/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner3ed83c12007-12-09 21:11:08 +0000210/// #line directive. This returns false if already at the specified line, true
211/// if some newlines were emitted.
Chris Lattner5dbefc62010-04-13 00:01:41 +0000212bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000213 // If this line is "close enough" to the original line, just print newlines,
214 // otherwise print a #line directive.
Daniel Dunbare7781312008-09-26 01:13:35 +0000215 if (LineNo-CurLine <= 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000216 if (LineNo-CurLine == 1)
Chris Lattner068529a2008-08-17 03:12:02 +0000217 OS << '\n';
Chris Lattner3ed83c12007-12-09 21:11:08 +0000218 else if (LineNo == CurLine)
Chandler Carruth46a32c22011-07-14 16:14:52 +0000219 return false; // Spelling line moved, but expansion line didn't.
Chris Lattner5f075822007-07-23 05:14:05 +0000220 else {
221 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattner068529a2008-08-17 03:12:02 +0000222 OS.write(NewLines, LineNo-CurLine);
Chris Lattner5f075822007-07-23 05:14:05 +0000223 }
Chris Lattnerbc6bcab2010-06-12 16:20:56 +0000224 } else if (!DisableLineMarkers) {
225 // Emit a #line or line marker.
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000226 WriteLineInfo(LineNo, 0, 0);
Chris Lattnerbc6bcab2010-06-12 16:20:56 +0000227 } else {
228 // Okay, we're in -P mode, which turns off line markers. However, we still
229 // need to emit a newline between tokens on different lines.
Jordan Rose127f6ee2012-06-15 23:33:51 +0000230 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
Mike Stump11289f42009-09-09 15:08:12 +0000231 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000232
Mike Stump11289f42009-09-09 15:08:12 +0000233 CurLine = LineNo;
Chris Lattner3ed83c12007-12-09 21:11:08 +0000234 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000235}
236
Jordan Rose127f6ee2012-06-15 23:33:51 +0000237bool
238PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
239 if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
Douglas Gregorc7d65762010-09-09 22:45:38 +0000240 OS << '\n';
241 EmittedTokensOnThisLine = false;
Jordan Rose127f6ee2012-06-15 23:33:51 +0000242 EmittedDirectiveOnThisLine = false;
243 if (ShouldUpdateCurrentLine)
244 ++CurLine;
Douglas Gregorc7d65762010-09-09 22:45:38 +0000245 return true;
246 }
247
248 return false;
249}
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000250
251/// FileChanged - Whenever the preprocessor enters or exits a #include file
252/// it invokes this handler. Update our conception of the current source
253/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000254void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
255 FileChangeReason Reason,
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000256 SrcMgr::CharacteristicKind NewFileType,
257 FileID PrevFID) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000258 // Unless we are exiting a #include, make sure to skip ahead to the line the
259 // #include directive was at.
Chris Lattner9d94f042010-04-13 00:06:42 +0000260 SourceManager &SourceMgr = SM;
Chris Lattner5dbefc62010-04-13 00:01:41 +0000261
262 PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
Douglas Gregor453b0122010-11-12 07:15:47 +0000263 if (UserLoc.isInvalid())
264 return;
265
Chris Lattnerc745cec2010-04-14 04:28:50 +0000266 unsigned NewLine = UserLoc.getLine();
Daniel Dunbard4352752010-08-24 22:44:13 +0000267
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000268 if (Reason == PPCallbacks::EnterFile) {
Douglas Gregor453b0122010-11-12 07:15:47 +0000269 SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
Chris Lattnerd8cc8842009-01-30 18:44:17 +0000270 if (IncludeLoc.isValid())
271 MoveToLine(IncludeLoc);
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000272 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Jordan Rose111c4a62013-04-17 19:09:18 +0000273 // GCC emits the # directive for this directive on the line AFTER the
274 // directive and emits a bunch of spaces that aren't needed. This is because
275 // otherwise we will emit a line marker for THIS line, which requires an
276 // extra blank line after the directive to avoid making all following lines
277 // off by one. We can do better by simply incrementing NewLine here.
278 NewLine += 1;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000279 }
Chris Lattner5dbefc62010-04-13 00:01:41 +0000280
281 CurLine = NewLine;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000282
Chris Lattner4c4a2452007-07-24 06:57:14 +0000283 CurFilename.clear();
Chris Lattner5dbefc62010-04-13 00:01:41 +0000284 CurFilename += UserLoc.getFilename();
Chris Lattner4c4a2452007-07-24 06:57:14 +0000285 Lexer::Stringify(CurFilename);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000286 FileType = NewFileType;
287
Eli Friedmanc52435b2013-01-09 03:16:42 +0000288 if (DisableLineMarkers) {
289 startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
290 return;
291 }
Daniel Dunbard4352752010-08-24 22:44:13 +0000292
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000293 if (!Initialized) {
294 WriteLineInfo(CurLine);
295 Initialized = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000296 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000297
Daniel Dunbar5d388752012-11-16 01:51:11 +0000298 // Do not emit an enter marker for the main file (which we expect is the first
299 // entered file). This matches gcc, and improves compatibility with some tools
300 // which track the # line markers as a way to determine when the preprocessed
301 // output is in the context of the main file.
302 if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
303 IsFirstFileEntered = true;
304 return;
305 }
306
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000307 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000308 case PPCallbacks::EnterFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000309 WriteLineInfo(CurLine, " 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000310 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000311 case PPCallbacks::ExitFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000312 WriteLineInfo(CurLine, " 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000313 break;
Mike Stump11289f42009-09-09 15:08:12 +0000314 case PPCallbacks::SystemHeaderPragma:
315 case PPCallbacks::RenameFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000316 WriteLineInfo(CurLine);
317 break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000318 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000319}
320
Argyrios Kyrtzidisa6444b12013-04-10 01:53:46 +0000321void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
322 const Token &IncludeTok,
323 StringRef FileName,
324 bool IsAngled,
325 CharSourceRange FilenameRange,
326 const FileEntry *File,
327 StringRef SearchPath,
328 StringRef RelativePath,
329 const Module *Imported) {
330 // When preprocessing, turn implicit imports into @imports.
331 // FIXME: This is a stop-gap until a more comprehensive "preprocessing with
332 // modules" solution is introduced.
333 if (Imported) {
334 startNewLineIfNeeded();
335 MoveToLine(HashLoc);
336 OS << "@import " << Imported->getFullModuleName() << ";"
337 << " /* clang -E: implicit import for \"" << File->getName() << "\" */";
338 }
339}
340
Chris Lattner5eef5072009-01-16 19:25:54 +0000341/// Ident - Handle #ident directives when read by the preprocessor.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000342///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000343void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000344 MoveToLine(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000345
Chris Lattner068529a2008-08-17 03:12:02 +0000346 OS.write("#ident ", strlen("#ident "));
347 OS.write(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000348 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000349}
350
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000351void PrintPPOutputPPCallbacks::PragmaCaptured(SourceLocation Loc,
352 StringRef Str) {
353 startNewLineIfNeeded();
354 MoveToLine(Loc);
355 OS << "#pragma captured";
356
357 setEmittedDirectiveOnThisLine();
358}
359
Chris Lattnercac63f32009-04-12 01:56:53 +0000360/// MacroDefined - This hook is called whenever a macro definition is seen.
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000361void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000362 const MacroDirective *MD) {
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000363 const MacroInfo *MI = MD->getMacroInfo();
Chris Lattnercac63f32009-04-12 01:56:53 +0000364 // Only print out macro definitions in -dD mode.
365 if (!DumpDefines ||
366 // Ignore __FILE__ etc.
367 MI->isBuiltinMacro()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000368
Chris Lattnercac63f32009-04-12 01:56:53 +0000369 MoveToLine(MI->getDefinitionLoc());
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000370 PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
Jordan Rose127f6ee2012-06-15 23:33:51 +0000371 setEmittedDirectiveOnThisLine();
Chris Lattnercac63f32009-04-12 01:56:53 +0000372}
373
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000374void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
Argyrios Kyrtzidisfead64b2013-02-24 00:05:14 +0000375 const MacroDirective *MD) {
Benjamin Kramerd05f31d2010-08-07 22:27:00 +0000376 // Only print out macro definitions in -dD mode.
377 if (!DumpDefines) return;
378
Craig Silverstein1a9ca212010-11-19 21:33:15 +0000379 MoveToLine(MacroNameTok.getLocation());
380 OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
Jordan Rose127f6ee2012-06-15 23:33:51 +0000381 setEmittedDirectiveOnThisLine();
Benjamin Kramerd05f31d2010-08-07 22:27:00 +0000382}
Chris Lattnercac63f32009-04-12 01:56:53 +0000383
Chris Lattner5eef5072009-01-16 19:25:54 +0000384void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000385 const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000386 const std::string &Str) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000387 startNewLineIfNeeded();
Chris Lattner5eef5072009-01-16 19:25:54 +0000388 MoveToLine(Loc);
389 OS << "#pragma comment(" << Kind->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000390
Chris Lattner5eef5072009-01-16 19:25:54 +0000391 if (!Str.empty()) {
392 OS << ", \"";
Mike Stump11289f42009-09-09 15:08:12 +0000393
Chris Lattner5eef5072009-01-16 19:25:54 +0000394 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
395 unsigned char Char = Str[i];
Jordan Rosea7d03842013-02-08 22:30:41 +0000396 if (isPrintable(Char) && Char != '\\' && Char != '"')
Chris Lattner5eef5072009-01-16 19:25:54 +0000397 OS << (char)Char;
398 else // Output anything hard as an octal escape.
399 OS << '\\'
400 << (char)('0'+ ((Char >> 6) & 7))
401 << (char)('0'+ ((Char >> 3) & 7))
402 << (char)('0'+ ((Char >> 0) & 7));
403 }
404 OS << '"';
405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner5eef5072009-01-16 19:25:54 +0000407 OS << ')';
Jordan Rose127f6ee2012-06-15 23:33:51 +0000408 setEmittedDirectiveOnThisLine();
Chris Lattner5eef5072009-01-16 19:25:54 +0000409}
410
Chris Lattner30c924b2010-06-26 17:11:39 +0000411void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
Andy Gibbs9c2ccd62013-04-17 16:16:16 +0000412 StringRef Namespace,
413 PragmaMessageKind Kind,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000414 StringRef Str) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000415 startNewLineIfNeeded();
Chris Lattner30c924b2010-06-26 17:11:39 +0000416 MoveToLine(Loc);
Andy Gibbs9c2ccd62013-04-17 16:16:16 +0000417 OS << "#pragma ";
418 if (!Namespace.empty())
419 OS << Namespace << ' ';
420 switch (Kind) {
421 case PMK_Message:
422 OS << "message(\"";
423 break;
424 case PMK_Warning:
425 OS << "warning(\"";
426 break;
427 case PMK_Error:
428 OS << "error(\"";
429 break;
430 }
Chris Lattner30c924b2010-06-26 17:11:39 +0000431
432 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
433 unsigned char Char = Str[i];
Jordan Rosea7d03842013-02-08 22:30:41 +0000434 if (isPrintable(Char) && Char != '\\' && Char != '"')
Chris Lattner30c924b2010-06-26 17:11:39 +0000435 OS << (char)Char;
436 else // Output anything hard as an octal escape.
437 OS << '\\'
438 << (char)('0'+ ((Char >> 6) & 7))
439 << (char)('0'+ ((Char >> 3) & 7))
440 << (char)('0'+ ((Char >> 0) & 7));
441 }
442 OS << '"';
443
444 OS << ')';
Jordan Rose127f6ee2012-06-15 23:33:51 +0000445 setEmittedDirectiveOnThisLine();
Chris Lattner30c924b2010-06-26 17:11:39 +0000446}
447
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000448void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
449 StringRef DebugType) {
450 startNewLineIfNeeded();
451 MoveToLine(Loc);
452
453 OS << "#pragma clang __debug ";
454 OS << DebugType;
455
456 setEmittedDirectiveOnThisLine();
457}
458
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000459void PrintPPOutputPPCallbacks::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000460PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000461 startNewLineIfNeeded();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000462 MoveToLine(Loc);
463 OS << "#pragma " << Namespace << " diagnostic push";
Jordan Rose127f6ee2012-06-15 23:33:51 +0000464 setEmittedDirectiveOnThisLine();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000465}
466
467void PrintPPOutputPPCallbacks::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000468PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000469 startNewLineIfNeeded();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000470 MoveToLine(Loc);
471 OS << "#pragma " << Namespace << " diagnostic pop";
Jordan Rose127f6ee2012-06-15 23:33:51 +0000472 setEmittedDirectiveOnThisLine();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000473}
474
475void PrintPPOutputPPCallbacks::
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000476PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000477 diag::Mapping Map, StringRef Str) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000478 startNewLineIfNeeded();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000479 MoveToLine(Loc);
480 OS << "#pragma " << Namespace << " diagnostic ";
Benjamin Kramer7ec12c92012-02-07 22:29:24 +0000481 switch (Map) {
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000482 case diag::MAP_WARNING:
483 OS << "warning";
484 break;
485 case diag::MAP_ERROR:
486 OS << "error";
487 break;
488 case diag::MAP_IGNORE:
489 OS << "ignored";
490 break;
491 case diag::MAP_FATAL:
492 OS << "fatal";
493 break;
494 }
495 OS << " \"" << Str << '"';
Jordan Rose127f6ee2012-06-15 23:33:51 +0000496 setEmittedDirectiveOnThisLine();
Douglas Gregor3bde9b12011-06-22 19:41:48 +0000497}
Chris Lattner5eef5072009-01-16 19:25:54 +0000498
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000499/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner3ed83c12007-12-09 21:11:08 +0000500/// is called for the first token on each new line. If this really is the start
501/// of a new logical line, handle it and return true, otherwise return false.
502/// This may not be the start of a logical line because the "start of line"
Chandler Carruth46a32c22011-07-14 16:14:52 +0000503/// marker is set for spelling lines, not expansion ones.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000504bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000505 // Figure out what line we went to and insert the appropriate number of
506 // newline characters.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000507 if (!MoveToLine(Tok.getLocation()))
508 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000510 // Print out space characters so that the first token on a line is
511 // indented for easy reading.
Chandler Carruth42f35f92011-07-25 20:57:57 +0000512 unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000514 // This hack prevents stuff like:
515 // #define HASH #
516 // HASH define foo bar
517 // From having the # character end up at column 1, which makes it so it
518 // is not handled as a #define next time through the preprocessor if in
519 // -fpreprocessed mode.
Chris Lattner3c69f122007-10-09 18:03:42 +0000520 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattner068529a2008-08-17 03:12:02 +0000521 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000522
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000523 // Otherwise, indent the appropriate number of spaces.
524 for (; ColNo > 1; --ColNo)
Chris Lattner068529a2008-08-17 03:12:02 +0000525 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner3ed83c12007-12-09 21:11:08 +0000527 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000528}
529
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000530void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
531 unsigned Len) {
532 unsigned NumNewlines = 0;
533 for (; Len; --Len, ++TokStr) {
534 if (*TokStr != '\n' &&
535 *TokStr != '\r')
536 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000538 ++NumNewlines;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000540 // If we have \n\r or \r\n, skip both and count as one line.
541 if (Len != 1 &&
542 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
543 TokStr[0] != TokStr[1])
544 ++TokStr, --Len;
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000547 if (NumNewlines == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattnera5e67752009-06-15 04:08:28 +0000549 CurLine += NumNewlines;
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000550}
551
552
Chris Lattner5de858c2006-07-04 19:04:44 +0000553namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000554struct UnknownPragmaHandler : public PragmaHandler {
555 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000556 PrintPPOutputPPCallbacks *Callbacks;
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattner87f267e2006-11-21 05:02:33 +0000558 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000559 : Prefix(prefix), Callbacks(callbacks) {}
Douglas Gregorc7d65762010-09-09 22:45:38 +0000560 virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
561 Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000562 // Figure out what line we went to and insert the appropriate number of
563 // newline characters.
Jordan Rose127f6ee2012-06-15 23:33:51 +0000564 Callbacks->startNewLineIfNeeded();
Chris Lattner87f267e2006-11-21 05:02:33 +0000565 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattner068529a2008-08-17 03:12:02 +0000566 Callbacks->OS.write(Prefix, strlen(Prefix));
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000567 // Read and print all of the pragma tokens.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000568 while (PragmaTok.isNot(tok::eod)) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000569 if (PragmaTok.hasLeadingSpace())
Chris Lattner068529a2008-08-17 03:12:02 +0000570 Callbacks->OS << ' ';
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000571 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattner068529a2008-08-17 03:12:02 +0000572 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000573 PP.LexUnexpandedToken(PragmaTok);
574 }
Jordan Rose127f6ee2012-06-15 23:33:51 +0000575 Callbacks->setEmittedDirectiveOnThisLine();
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000576 }
577};
Chris Lattner5de858c2006-07-04 19:04:44 +0000578} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000579
Chris Lattner4418ce12007-07-23 06:09:34 +0000580
Chris Lattnerfc001b02009-02-06 05:56:11 +0000581static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
582 PrintPPOutputPPCallbacks *Callbacks,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000583 raw_ostream &OS) {
Jordan Rose67b66232013-03-05 23:54:55 +0000584 bool DropComments = PP.getLangOpts().TraditionalCPP &&
585 !PP.getCommentRetentionState();
586
Benjamin Kramer718f7222010-02-27 18:02:51 +0000587 char Buffer[256];
Chris Lattnerbba37f42010-06-15 21:06:38 +0000588 Token PrevPrevTok, PrevTok;
589 PrevPrevTok.startToken();
590 PrevTok.startToken();
Chris Lattner3ff2e692007-10-10 20:45:16 +0000591 while (1) {
Jordan Rose127f6ee2012-06-15 23:33:51 +0000592 if (Callbacks->hasEmittedDirectiveOnThisLine()) {
593 Callbacks->startNewLineIfNeeded();
594 Callbacks->MoveToLine(Tok.getLocation());
595 }
Mike Stump11289f42009-09-09 15:08:12 +0000596
Chris Lattner67c38482006-07-04 23:24:26 +0000597 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000598 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
599 // done.
Mike Stump11289f42009-09-09 15:08:12 +0000600 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-07-23 06:09:34 +0000601 // If we haven't emitted a token on this line yet, PrevTok isn't
602 // useful to look at and no concatenation could happen anyway.
Chris Lattnerd63c8a52007-07-23 23:21:34 +0000603 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattner4418ce12007-07-23 06:09:34 +0000604 // Don't print "-" next to "-", it would form "--".
Chris Lattner0384e6352010-04-14 03:57:19 +0000605 Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
Chris Lattner068529a2008-08-17 03:12:02 +0000606 OS << ' ';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Jordan Rose67b66232013-03-05 23:54:55 +0000609 if (DropComments && Tok.is(tok::comment)) {
610 // Skip comments. Normally the preprocessor does not generate
611 // tok::comment nodes at all when not keeping comments, but under
612 // -traditional-cpp the lexer keeps /all/ whitespace, including comments.
613 SourceLocation StartLoc = Tok.getLocation();
614 Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
615 } else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Benjamin Kramer718f7222010-02-27 18:02:51 +0000616 OS << II->getName();
617 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
618 Tok.getLiteralData()) {
619 OS.write(Tok.getLiteralData(), Tok.getLength());
620 } else if (Tok.getLength() < 256) {
621 const char *TokPtr = Buffer;
622 unsigned Len = PP.getSpelling(Tok, TokPtr);
623 OS.write(TokPtr, Len);
Mike Stump11289f42009-09-09 15:08:12 +0000624
Benjamin Kramer718f7222010-02-27 18:02:51 +0000625 // Tokens that can contain embedded newlines need to adjust our current
626 // line number.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000627 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
Benjamin Kramer718f7222010-02-27 18:02:51 +0000628 Callbacks->HandleNewlinesInToken(TokPtr, Len);
629 } else {
630 std::string S = PP.getSpelling(Tok);
631 OS.write(&S[0], S.size());
632
633 // Tokens that can contain embedded newlines need to adjust our current
634 // line number.
Jordan Rosecb8a1ac2013-02-21 18:53:19 +0000635 if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
Benjamin Kramer718f7222010-02-27 18:02:51 +0000636 Callbacks->HandleNewlinesInToken(&S[0], S.size());
637 }
Jordan Rose127f6ee2012-06-15 23:33:51 +0000638 Callbacks->setEmittedTokensOnThisLine();
Mike Stump11289f42009-09-09 15:08:12 +0000639
Chris Lattner3ff2e692007-10-10 20:45:16 +0000640 if (Tok.is(tok::eof)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner0384e6352010-04-14 03:57:19 +0000642 PrevPrevTok = PrevTok;
Chris Lattner3ff2e692007-10-10 20:45:16 +0000643 PrevTok = Tok;
644 PP.Lex(Tok);
645 }
Chris Lattnerfc001b02009-02-06 05:56:11 +0000646}
647
Dmitri Gribenko049a4ff2013-01-14 00:36:42 +0000648typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
Benjamin Kramerfb5f40f2010-01-19 17:42:20 +0000649static int MacroIDCompare(const void* a, const void* b) {
650 const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a);
651 const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b);
652 return LHS->first->getName().compare(RHS->first->getName());
Chris Lattner1ec246d2009-02-10 22:28:19 +0000653}
Chris Lattnerfc001b02009-02-06 05:56:11 +0000654
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000655static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
Daniel Dunbard839e772010-06-11 20:10:12 +0000656 // Ignore unknown pragmas.
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000657 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbard839e772010-06-11 20:10:12 +0000658
Eli Friedman26c672b2009-05-19 01:32:34 +0000659 // -dM mode just scans and ignores all tokens in the files, then dumps out
660 // the macro table at the end.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000661 PP.EnterMainSourceFile();
Eli Friedman26c672b2009-05-19 01:32:34 +0000662
663 Token Tok;
664 do PP.Lex(Tok);
665 while (Tok.isNot(tok::eof));
666
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000667 SmallVector<id_macro_pair, 128> MacrosByID;
668 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
669 I != E; ++I) {
670 if (I->first->hasMacroDefinition())
Argyrios Kyrtzidisb6210df2013-03-26 17:17:01 +0000671 MacrosByID.push_back(id_macro_pair(I->first, I->second->getMacroInfo()));
Alexander Kornienko8b3f6232012-08-29 00:20:03 +0000672 }
Benjamin Kramerfb5f40f2010-01-19 17:42:20 +0000673 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
Eli Friedman26c672b2009-05-19 01:32:34 +0000674
675 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
676 MacroInfo &MI = *MacrosByID[i].second;
Mike Stump11289f42009-09-09 15:08:12 +0000677 // Ignore computed macros like __LINE__ and friends.
Eli Friedman26c672b2009-05-19 01:32:34 +0000678 if (MI.isBuiltinMacro()) continue;
679
680 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
Benjamin Kramerfb5f40f2010-01-19 17:42:20 +0000681 *OS << '\n';
Eli Friedman26c672b2009-05-19 01:32:34 +0000682 }
683}
684
Chris Lattnerfc001b02009-02-06 05:56:11 +0000685/// DoPrintPreprocessedInput - This implements -E mode.
686///
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000687void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
Daniel Dunbar22bdabf2009-11-11 10:07:44 +0000688 const PreprocessorOutputOptions &Opts) {
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000689 // Show macros with no output is handled specially.
690 if (!Opts.ShowCPP) {
691 assert(Opts.ShowMacros && "Not yet implemented!");
692 DoPrintMacros(PP, OS);
693 return;
694 }
695
Chris Lattnerfc001b02009-02-06 05:56:11 +0000696 // Inform the preprocessor whether we want it to retain comments or not, due
697 // to -C or -CC.
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000698 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
Eli Friedmanfcb57d52009-05-19 01:02:07 +0000699
Eli Friedman6af494b2009-05-19 03:06:47 +0000700 PrintPPOutputPPCallbacks *Callbacks =
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000701 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
Daniel Dunbar27734fd2011-02-02 15:41:17 +0000702 Opts.ShowMacros);
Argyrios Kyrtzidis36745fd2010-07-13 09:07:17 +0000703 PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
Chris Lattner49609d52010-11-10 01:00:49 +0000704 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
705 PP.AddPragmaHandler("clang",
706 new UnknownPragmaHandler("#pragma clang", Callbacks));
Chris Lattnerfc001b02009-02-06 05:56:11 +0000707
Kovarththanan Rajaratnam752a1242010-03-07 07:30:06 +0000708 PP.addPPCallbacks(Callbacks);
Chris Lattnerfc001b02009-02-06 05:56:11 +0000709
Eli Friedman26c672b2009-05-19 01:32:34 +0000710 // After we have configured the preprocessor, enter the main file.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000711 PP.EnterMainSourceFile();
Chris Lattnerfc001b02009-02-06 05:56:11 +0000712
Eli Friedman26c672b2009-05-19 01:32:34 +0000713 // Consume all of the tokens that come from the predefines buffer. Those
714 // should not be emitted into the output and are guaranteed to be at the
715 // start.
716 const SourceManager &SourceMgr = PP.getSourceManager();
717 Token Tok;
Douglas Gregor453b0122010-11-12 07:15:47 +0000718 do {
719 PP.Lex(Tok);
720 if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
721 break;
722
723 PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
724 if (PLoc.isInvalid())
725 break;
726
727 if (strcmp(PLoc.getFilename(), "<built-in>"))
728 break;
729 } while (true);
Chris Lattnerfc001b02009-02-06 05:56:11 +0000730
Eli Friedman26c672b2009-05-19 01:32:34 +0000731 // Read all the preprocessed tokens, printing them out to the stream.
732 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
733 *OS << '\n';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000734}