blob: e0186abeb49cbcdf5e74bbf1220b690df42bac99 [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"
Daniel Dunbar531f6c62009-11-11 10:07:22 +000016#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Frontend/PreprocessorOutputOptions.h"
Chris Lattner1630c3c2009-02-06 06:45:26 +000019#include "clang/Lex/MacroInfo.h"
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +000020#include "clang/Lex/PPCallbacks.h"
Chris Lattner09e3cdf2006-07-04 19:04:05 +000021#include "clang/Lex/Pragma.h"
Daniel Dunbar531f6c62009-11-11 10:07:22 +000022#include "clang/Lex/Preprocessor.h"
Chris Lattner644d4522009-02-13 00:46:04 +000023#include "clang/Lex/TokenConcatenation.h"
Chris Lattner4c4a2452007-07-24 06:57:14 +000024#include "llvm/ADT/SmallString.h"
Chris Lattnerf46be6c2006-07-04 22:19:33 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/Config/config.h"
Chris Lattnerb5a92f82008-08-17 01:47:12 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000028#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000029using namespace clang;
30
Chris Lattnercac63f32009-04-12 01:56:53 +000031/// PrintMacroDefinition - Print a macro definition in a form that will be
32/// properly accepted back as a definition.
33static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
34 Preprocessor &PP, llvm::raw_ostream &OS) {
35 OS << "#define " << II.getName();
Mike Stump11289f42009-09-09 15:08:12 +000036
Chris Lattnercac63f32009-04-12 01:56:53 +000037 if (MI.isFunctionLike()) {
38 OS << '(';
39 if (MI.arg_empty())
40 ;
Mike Stump11289f42009-09-09 15:08:12 +000041 else if (MI.getNumArgs() == 1)
Chris Lattnercac63f32009-04-12 01:56:53 +000042 OS << (*MI.arg_begin())->getName();
43 else {
44 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
45 OS << (*AI++)->getName();
46 while (AI != E)
47 OS << ',' << (*AI++)->getName();
48 }
Mike Stump11289f42009-09-09 15:08:12 +000049
Chris Lattnercac63f32009-04-12 01:56:53 +000050 if (MI.isVariadic()) {
51 if (!MI.arg_empty())
52 OS << ',';
53 OS << "...";
54 }
55 OS << ')';
56 }
Mike Stump11289f42009-09-09 15:08:12 +000057
Chris Lattnercac63f32009-04-12 01:56:53 +000058 // GCC always emits a space, even if the macro body is empty. However, do not
59 // want to emit two spaces if the first token has a leading space.
60 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
61 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattnercac63f32009-04-12 01:56:53 +000063 llvm::SmallVector<char, 128> SpellingBuffer;
64 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
65 I != E; ++I) {
66 if (I->hasLeadingSpace())
67 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +000068
Chris Lattnercac63f32009-04-12 01:56:53 +000069 // Make sure we have enough space in the spelling buffer.
Ted Kremenek66791d52009-11-03 06:18:05 +000070 if (I->getLength() > SpellingBuffer.size())
Chris Lattnercac63f32009-04-12 01:56:53 +000071 SpellingBuffer.resize(I->getLength());
Jay Foad7d0479f2009-05-21 09:52:38 +000072 const char *Buffer = SpellingBuffer.data();
Chris Lattnercac63f32009-04-12 01:56:53 +000073 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
74 OS.write(Buffer, SpellingLen);
75 }
76}
77
Chris Lattnerf46be6c2006-07-04 22:19:33 +000078//===----------------------------------------------------------------------===//
79// Preprocessed token printer
80//===----------------------------------------------------------------------===//
81
Chris Lattner87f267e2006-11-21 05:02:33 +000082namespace {
83class PrintPPOutputPPCallbacks : public PPCallbacks {
84 Preprocessor &PP;
Chris Lattner644d4522009-02-13 00:46:04 +000085 TokenConcatenation ConcatInfo;
Chris Lattner068529a2008-08-17 03:12:02 +000086public:
87 llvm::raw_ostream &OS;
88private:
Chris Lattner87f267e2006-11-21 05:02:33 +000089 unsigned CurLine;
Chris Lattner87f267e2006-11-21 05:02:33 +000090 bool EmittedTokensOnThisLine;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +000091 bool EmittedMacroOnThisLine;
Chris Lattner66a740e2008-10-27 01:19:25 +000092 SrcMgr::CharacteristicKind FileType;
Chris Lattner4c4a2452007-07-24 06:57:14 +000093 llvm::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;
Chris Lattner87f267e2006-11-21 05:02:33 +000098public:
Eli Friedman6af494b2009-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) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000103 CurLine = 0;
Chris Lattner4c4a2452007-07-24 06:57:14 +0000104 CurFilename += "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000105 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000106 EmittedMacroOnThisLine = false;
Chris Lattnerb03dc762008-09-26 21:18:42 +0000107 FileType = SrcMgr::C_User;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000108 Initialized = false;
Chris Lattner76b44452009-12-07 01:42:56 +0000109
110 // If we're in microsoft mode, use normal #line instead of line markers.
111 UseLineDirective = PP.getLangOptions().Microsoft;
Chris Lattner87f267e2006-11-21 05:02:33 +0000112 }
Mike Stump11289f42009-09-09 15:08:12 +0000113
Chris Lattner87f267e2006-11-21 05:02:33 +0000114 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000115 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner87f267e2006-11-21 05:02:33 +0000117 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner66a740e2008-10-27 01:19:25 +0000118 SrcMgr::CharacteristicKind FileType);
Chris Lattner87f267e2006-11-21 05:02:33 +0000119 virtual void Ident(SourceLocation Loc, const std::string &str);
Mike Stump11289f42009-09-09 15:08:12 +0000120 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000121 const std::string &Str);
122
Chris Lattner87f267e2006-11-21 05:02:33 +0000123
Chris Lattner3ed83c12007-12-09 21:11:08 +0000124 bool HandleFirstTokOnLine(Token &Tok);
125 bool MoveToLine(SourceLocation Loc);
Chris Lattner644d4522009-02-13 00:46:04 +0000126 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
127 return ConcatInfo.AvoidConcat(PrevTok, Tok);
128 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000129 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Mike Stump11289f42009-09-09 15:08:12 +0000130
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000131 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattnercac63f32009-04-12 01:56:53 +0000133 /// MacroDefined - This hook is called whenever a macro definition is seen.
134 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
Mike Stump11289f42009-09-09 15:08:12 +0000135
Chris Lattner87f267e2006-11-21 05:02:33 +0000136};
Chris Lattner21632652008-04-08 04:16:20 +0000137} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000138
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000139void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
140 const char *Extra,
141 unsigned ExtraLen) {
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000142 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000143 OS << '\n';
144 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000145 EmittedMacroOnThisLine = false;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000146 }
147
Chris Lattner76b44452009-12-07 01:42:56 +0000148 // Emit #line directives or GNU line markers depending on what mode we're in.
149 if (UseLineDirective) {
150 OS << "#line" << ' ' << LineNo << ' ' << '"';
151 OS.write(&CurFilename[0], CurFilename.size());
152 OS << '"';
153 } else {
154 OS << '#' << ' ' << LineNo << ' ' << '"';
155 OS.write(&CurFilename[0], CurFilename.size());
156 OS << '"';
157
Steve Naroff66aaa392009-12-06 01:02:14 +0000158 if (ExtraLen)
159 OS.write(Extra, ExtraLen);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000160
Steve Naroff66aaa392009-12-06 01:02:14 +0000161 if (FileType == SrcMgr::C_System)
162 OS.write(" 3", 2);
163 else if (FileType == SrcMgr::C_ExternCSystem)
164 OS.write(" 3 4", 4);
165 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000166 OS << '\n';
167}
168
Chris Lattner728b4dc2006-07-04 21:28:37 +0000169/// MoveToLine - Move the output to the source line specified by the location
170/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner3ed83c12007-12-09 21:11:08 +0000171/// #line directive. This returns false if already at the specified line, true
172/// if some newlines were emitted.
173bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattner8a425862009-01-16 07:36:28 +0000174 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000175
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000176 if (DisableLineMarkers) {
Chris Lattner3ed83c12007-12-09 21:11:08 +0000177 if (LineNo == CurLine) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner3ed83c12007-12-09 21:11:08 +0000179 CurLine = LineNo;
Mike Stump11289f42009-09-09 15:08:12 +0000180
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000181 if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine)
Chris Lattner3ed83c12007-12-09 21:11:08 +0000182 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner068529a2008-08-17 03:12:02 +0000184 OS << '\n';
Chris Lattner3ed83c12007-12-09 21:11:08 +0000185 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000186 EmittedMacroOnThisLine = false;
Chris Lattner3ed83c12007-12-09 21:11:08 +0000187 return true;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000188 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000189
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000190 // If this line is "close enough" to the original line, just print newlines,
191 // otherwise print a #line directive.
Daniel Dunbare7781312008-09-26 01:13:35 +0000192 if (LineNo-CurLine <= 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000193 if (LineNo-CurLine == 1)
Chris Lattner068529a2008-08-17 03:12:02 +0000194 OS << '\n';
Chris Lattner3ed83c12007-12-09 21:11:08 +0000195 else if (LineNo == CurLine)
Chris Lattner8a425862009-01-16 07:36:28 +0000196 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner5f075822007-07-23 05:14:05 +0000197 else {
198 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattner068529a2008-08-17 03:12:02 +0000199 OS.write(NewLines, LineNo-CurLine);
Chris Lattner5f075822007-07-23 05:14:05 +0000200 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000201 } else {
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000202 WriteLineInfo(LineNo, 0, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000203 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000204
Mike Stump11289f42009-09-09 15:08:12 +0000205 CurLine = LineNo;
Chris Lattner3ed83c12007-12-09 21:11:08 +0000206 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000207}
208
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000209
210/// FileChanged - Whenever the preprocessor enters or exits a #include file
211/// it invokes this handler. Update our conception of the current source
212/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000213void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
214 FileChangeReason Reason,
Chris Lattner66a740e2008-10-27 01:19:25 +0000215 SrcMgr::CharacteristicKind NewFileType) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000216 // Unless we are exiting a #include, make sure to skip ahead to the line the
217 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000218 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000219 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerd8cc8842009-01-30 18:44:17 +0000220 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
221 if (IncludeLoc.isValid())
222 MoveToLine(IncludeLoc);
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000223 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000224 MoveToLine(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000226 // TODO GCC emits the # directive for this directive on the line AFTER the
227 // directive and emits a bunch of spaces that aren't needed. Emulate this
228 // strange behavior.
229 }
Mike Stump11289f42009-09-09 15:08:12 +0000230
Chris Lattner8a425862009-01-16 07:36:28 +0000231 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner839150e2009-03-27 17:13:49 +0000232 // FIXME: Should use presumed line #!
Chris Lattner88ea93e2009-02-04 01:06:56 +0000233 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000234
Chris Lattner3ed83c12007-12-09 21:11:08 +0000235 if (DisableLineMarkers) return;
236
Chris Lattner4c4a2452007-07-24 06:57:14 +0000237 CurFilename.clear();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000238 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattner4c4a2452007-07-24 06:57:14 +0000239 Lexer::Stringify(CurFilename);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000240 FileType = NewFileType;
241
242 if (!Initialized) {
243 WriteLineInfo(CurLine);
244 Initialized = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000245 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000246
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000247 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000248 case PPCallbacks::EnterFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000249 WriteLineInfo(CurLine, " 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000250 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000251 case PPCallbacks::ExitFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000252 WriteLineInfo(CurLine, " 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000253 break;
Mike Stump11289f42009-09-09 15:08:12 +0000254 case PPCallbacks::SystemHeaderPragma:
255 case PPCallbacks::RenameFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000256 WriteLineInfo(CurLine);
257 break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000258 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000259}
260
Chris Lattner5eef5072009-01-16 19:25:54 +0000261/// Ident - Handle #ident directives when read by the preprocessor.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000262///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000263void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000264 MoveToLine(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner068529a2008-08-17 03:12:02 +0000266 OS.write("#ident ", strlen("#ident "));
267 OS.write(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000268 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000269}
270
Chris Lattnercac63f32009-04-12 01:56:53 +0000271/// MacroDefined - This hook is called whenever a macro definition is seen.
272void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
273 const MacroInfo *MI) {
274 // Only print out macro definitions in -dD mode.
275 if (!DumpDefines ||
276 // Ignore __FILE__ etc.
277 MI->isBuiltinMacro()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000278
Chris Lattnercac63f32009-04-12 01:56:53 +0000279 MoveToLine(MI->getDefinitionLoc());
280 PrintMacroDefinition(*II, *MI, PP, OS);
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000281 EmittedMacroOnThisLine = true;
Chris Lattnercac63f32009-04-12 01:56:53 +0000282}
283
284
Chris Lattner5eef5072009-01-16 19:25:54 +0000285void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000286 const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000287 const std::string &Str) {
288 MoveToLine(Loc);
289 OS << "#pragma comment(" << Kind->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000290
Chris Lattner5eef5072009-01-16 19:25:54 +0000291 if (!Str.empty()) {
292 OS << ", \"";
Mike Stump11289f42009-09-09 15:08:12 +0000293
Chris Lattner5eef5072009-01-16 19:25:54 +0000294 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
295 unsigned char Char = Str[i];
Chris Lattnerdde6eb32009-01-16 22:13:37 +0000296 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattner5eef5072009-01-16 19:25:54 +0000297 OS << (char)Char;
298 else // Output anything hard as an octal escape.
299 OS << '\\'
300 << (char)('0'+ ((Char >> 6) & 7))
301 << (char)('0'+ ((Char >> 3) & 7))
302 << (char)('0'+ ((Char >> 0) & 7));
303 }
304 OS << '"';
305 }
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattner5eef5072009-01-16 19:25:54 +0000307 OS << ')';
308 EmittedTokensOnThisLine = true;
309}
310
311
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000312/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner3ed83c12007-12-09 21:11:08 +0000313/// is called for the first token on each new line. If this really is the start
314/// of a new logical line, handle it and return true, otherwise return false.
315/// This may not be the start of a logical line because the "start of line"
Chris Lattner8a425862009-01-16 07:36:28 +0000316/// marker is set for spelling lines, not instantiation ones.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000317bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000318 // Figure out what line we went to and insert the appropriate number of
319 // newline characters.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000320 if (!MoveToLine(Tok.getLocation()))
321 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000323 // Print out space characters so that the first token on a line is
324 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000325 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattner8a425862009-01-16 07:36:28 +0000326 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000327
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000328 // This hack prevents stuff like:
329 // #define HASH #
330 // HASH define foo bar
331 // From having the # character end up at column 1, which makes it so it
332 // is not handled as a #define next time through the preprocessor if in
333 // -fpreprocessed mode.
Chris Lattner3c69f122007-10-09 18:03:42 +0000334 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattner068529a2008-08-17 03:12:02 +0000335 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000336
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000337 // Otherwise, indent the appropriate number of spaces.
338 for (; ColNo > 1; --ColNo)
Chris Lattner068529a2008-08-17 03:12:02 +0000339 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000340
Chris Lattner3ed83c12007-12-09 21:11:08 +0000341 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000342}
343
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000344void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
345 unsigned Len) {
346 unsigned NumNewlines = 0;
347 for (; Len; --Len, ++TokStr) {
348 if (*TokStr != '\n' &&
349 *TokStr != '\r')
350 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000352 ++NumNewlines;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000354 // If we have \n\r or \r\n, skip both and count as one line.
355 if (Len != 1 &&
356 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
357 TokStr[0] != TokStr[1])
358 ++TokStr, --Len;
359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000361 if (NumNewlines == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000362
Chris Lattnera5e67752009-06-15 04:08:28 +0000363 CurLine += NumNewlines;
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000364}
365
366
Chris Lattner5de858c2006-07-04 19:04:44 +0000367namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000368struct UnknownPragmaHandler : public PragmaHandler {
369 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000370 PrintPPOutputPPCallbacks *Callbacks;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Chris Lattner87f267e2006-11-21 05:02:33 +0000372 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
373 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000374 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000375 // Figure out what line we went to and insert the appropriate number of
376 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000377 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattner068529a2008-08-17 03:12:02 +0000378 Callbacks->OS.write(Prefix, strlen(Prefix));
Mike Stump11289f42009-09-09 15:08:12 +0000379
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000380 // Read and print all of the pragma tokens.
Chris Lattner3c69f122007-10-09 18:03:42 +0000381 while (PragmaTok.isNot(tok::eom)) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000382 if (PragmaTok.hasLeadingSpace())
Chris Lattner068529a2008-08-17 03:12:02 +0000383 Callbacks->OS << ' ';
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000384 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattner068529a2008-08-17 03:12:02 +0000385 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000386 PP.LexUnexpandedToken(PragmaTok);
387 }
Chris Lattner068529a2008-08-17 03:12:02 +0000388 Callbacks->OS << '\n';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000389 }
390};
Chris Lattner5de858c2006-07-04 19:04:44 +0000391} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000392
Chris Lattner4418ce12007-07-23 06:09:34 +0000393
Chris Lattnerfc001b02009-02-06 05:56:11 +0000394static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
395 PrintPPOutputPPCallbacks *Callbacks,
396 llvm::raw_ostream &OS) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000397 char Buffer[256];
Chris Lattnerfc001b02009-02-06 05:56:11 +0000398 Token PrevTok;
Chris Lattner3ff2e692007-10-10 20:45:16 +0000399 while (1) {
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner67c38482006-07-04 23:24:26 +0000401 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000402 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
403 // done.
Mike Stump11289f42009-09-09 15:08:12 +0000404 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-07-23 06:09:34 +0000405 // If we haven't emitted a token on this line yet, PrevTok isn't
406 // useful to look at and no concatenation could happen anyway.
Chris Lattnerd63c8a52007-07-23 23:21:34 +0000407 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattner4418ce12007-07-23 06:09:34 +0000408 // Don't print "-" next to "-", it would form "--".
409 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattner068529a2008-08-17 03:12:02 +0000410 OS << ' ';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000411 }
Mike Stump11289f42009-09-09 15:08:12 +0000412
Chris Lattner0af98232007-07-23 06:14:36 +0000413 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000414 OS << II->getName();
Chris Lattner9892ea22009-01-26 19:33:54 +0000415 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
416 Tok.getLiteralData()) {
417 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner0af98232007-07-23 06:14:36 +0000418 } else if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000419 const char *TokPtr = Buffer;
420 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattner068529a2008-08-17 03:12:02 +0000421 OS.write(TokPtr, Len);
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000423 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump11289f42009-09-09 15:08:12 +0000424 // line number.
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000425 if (Tok.getKind() == tok::comment)
426 Callbacks->HandleNewlinesInToken(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000427 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000428 std::string S = PP.getSpelling(Tok);
Chris Lattner068529a2008-08-17 03:12:02 +0000429 OS.write(&S[0], S.size());
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000431 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump11289f42009-09-09 15:08:12 +0000432 // line number.
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000433 if (Tok.getKind() == tok::comment)
434 Callbacks->HandleNewlinesInToken(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000435 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000436 Callbacks->SetEmittedTokensOnThisLine();
Mike Stump11289f42009-09-09 15:08:12 +0000437
Chris Lattner3ff2e692007-10-10 20:45:16 +0000438 if (Tok.is(tok::eof)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner3ff2e692007-10-10 20:45:16 +0000440 PrevTok = Tok;
441 PP.Lex(Tok);
442 }
Chris Lattnerfc001b02009-02-06 05:56:11 +0000443}
444
Chris Lattner1ec246d2009-02-10 22:28:19 +0000445namespace {
446 struct SortMacrosByID {
447 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
448 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000449 return LHS.first->getName() < RHS.first->getName();
Chris Lattner1ec246d2009-02-10 22:28:19 +0000450 }
451 };
452}
Chris Lattnerfc001b02009-02-06 05:56:11 +0000453
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000454static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
Eli Friedman26c672b2009-05-19 01:32:34 +0000455 // -dM mode just scans and ignores all tokens in the files, then dumps out
456 // the macro table at the end.
457 PP.EnterMainSourceFile();
458
459 Token Tok;
460 do PP.Lex(Tok);
461 while (Tok.isNot(tok::eof));
462
463 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
464 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
465 I != E; ++I)
466 MacrosByID.push_back(*I);
467 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
468
469 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
470 MacroInfo &MI = *MacrosByID[i].second;
Mike Stump11289f42009-09-09 15:08:12 +0000471 // Ignore computed macros like __LINE__ and friends.
Eli Friedman26c672b2009-05-19 01:32:34 +0000472 if (MI.isBuiltinMacro()) continue;
473
474 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
475 *OS << "\n";
476 }
477}
478
Chris Lattnerfc001b02009-02-06 05:56:11 +0000479/// DoPrintPreprocessedInput - This implements -E mode.
480///
Eli Friedman6af494b2009-05-19 03:06:47 +0000481void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
Daniel Dunbar22bdabf2009-11-11 10:07:44 +0000482 const PreprocessorOutputOptions &Opts) {
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000483 // Show macros with no output is handled specially.
484 if (!Opts.ShowCPP) {
485 assert(Opts.ShowMacros && "Not yet implemented!");
486 DoPrintMacros(PP, OS);
487 return;
488 }
489
Chris Lattnerfc001b02009-02-06 05:56:11 +0000490 // Inform the preprocessor whether we want it to retain comments or not, due
491 // to -C or -CC.
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000492 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
Eli Friedmanfcb57d52009-05-19 01:02:07 +0000493
494 OS->SetBufferSize(64*1024);
Chris Lattnercac63f32009-04-12 01:56:53 +0000495
Eli Friedman6af494b2009-05-19 03:06:47 +0000496 PrintPPOutputPPCallbacks *Callbacks =
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000497 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
498 Opts.ShowMacros);
Eli Friedman26c672b2009-05-19 01:32:34 +0000499 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
500 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
501 Callbacks));
Chris Lattnerfc001b02009-02-06 05:56:11 +0000502
Eli Friedman26c672b2009-05-19 01:32:34 +0000503 PP.setPPCallbacks(Callbacks);
Chris Lattnerfc001b02009-02-06 05:56:11 +0000504
Eli Friedman26c672b2009-05-19 01:32:34 +0000505 // After we have configured the preprocessor, enter the main file.
506 PP.EnterMainSourceFile();
Chris Lattnerfc001b02009-02-06 05:56:11 +0000507
Eli Friedman26c672b2009-05-19 01:32:34 +0000508 // Consume all of the tokens that come from the predefines buffer. Those
509 // should not be emitted into the output and are guaranteed to be at the
510 // start.
511 const SourceManager &SourceMgr = PP.getSourceManager();
512 Token Tok;
513 do PP.Lex(Tok);
514 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
515 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
516 "<built-in>"));
Chris Lattnerfc001b02009-02-06 05:56:11 +0000517
Eli Friedman26c672b2009-05-19 01:32:34 +0000518 // Read all the preprocessed tokens, printing them out to the stream.
519 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
520 *OS << '\n';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000521}
522