blob: ff8dfcd3c843cb8440512ca3ab275172d656fdcd [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 Lattner87f267e2006-11-21 05:02:33 +000097public:
Eli Friedman6af494b2009-05-19 03:06:47 +000098 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
99 bool lineMarkers, bool defines)
100 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
101 DumpDefines(defines) {
Chris Lattner87f267e2006-11-21 05:02:33 +0000102 CurLine = 0;
Chris Lattner4c4a2452007-07-24 06:57:14 +0000103 CurFilename += "<uninit>";
Chris Lattner87f267e2006-11-21 05:02:33 +0000104 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000105 EmittedMacroOnThisLine = false;
Chris Lattnerb03dc762008-09-26 21:18:42 +0000106 FileType = SrcMgr::C_User;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000107 Initialized = false;
Chris Lattner87f267e2006-11-21 05:02:33 +0000108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Chris Lattner87f267e2006-11-21 05:02:33 +0000110 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattner4418ce12007-07-23 06:09:34 +0000111 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattner87f267e2006-11-21 05:02:33 +0000113 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner66a740e2008-10-27 01:19:25 +0000114 SrcMgr::CharacteristicKind FileType);
Chris Lattner87f267e2006-11-21 05:02:33 +0000115 virtual void Ident(SourceLocation Loc, const std::string &str);
Mike Stump11289f42009-09-09 15:08:12 +0000116 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000117 const std::string &Str);
118
Chris Lattner87f267e2006-11-21 05:02:33 +0000119
Chris Lattner3ed83c12007-12-09 21:11:08 +0000120 bool HandleFirstTokOnLine(Token &Tok);
121 bool MoveToLine(SourceLocation Loc);
Chris Lattner644d4522009-02-13 00:46:04 +0000122 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
123 return ConcatInfo.AvoidConcat(PrevTok, Tok);
124 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000125 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Mike Stump11289f42009-09-09 15:08:12 +0000126
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000127 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
Mike Stump11289f42009-09-09 15:08:12 +0000128
Chris Lattnercac63f32009-04-12 01:56:53 +0000129 /// MacroDefined - This hook is called whenever a macro definition is seen.
130 void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
Mike Stump11289f42009-09-09 15:08:12 +0000131
Chris Lattner87f267e2006-11-21 05:02:33 +0000132};
Chris Lattner21632652008-04-08 04:16:20 +0000133} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000134
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000135void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
136 const char *Extra,
137 unsigned ExtraLen) {
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000138 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000139 OS << '\n';
140 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000141 EmittedMacroOnThisLine = false;
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000142 }
143
Steve Naroff66aaa392009-12-06 01:02:14 +0000144 OS << '#';
145 if (PP.getLangOptions().Microsoft)
146 OS << "line";
147 OS << ' ' << LineNo << ' ' << '"';
148
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000149 OS.write(&CurFilename[0], CurFilename.size());
150 OS << '"';
Mike Stump11289f42009-09-09 15:08:12 +0000151
Steve Naroff66aaa392009-12-06 01:02:14 +0000152 if (!PP.getLangOptions().Microsoft) {
153 if (ExtraLen)
154 OS.write(Extra, ExtraLen);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000155
Steve Naroff66aaa392009-12-06 01:02:14 +0000156 if (FileType == SrcMgr::C_System)
157 OS.write(" 3", 2);
158 else if (FileType == SrcMgr::C_ExternCSystem)
159 OS.write(" 3 4", 4);
160 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000161 OS << '\n';
162}
163
Chris Lattner728b4dc2006-07-04 21:28:37 +0000164/// MoveToLine - Move the output to the source line specified by the location
165/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner3ed83c12007-12-09 21:11:08 +0000166/// #line directive. This returns false if already at the specified line, true
167/// if some newlines were emitted.
168bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattner8a425862009-01-16 07:36:28 +0000169 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000170
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000171 if (DisableLineMarkers) {
Chris Lattner3ed83c12007-12-09 21:11:08 +0000172 if (LineNo == CurLine) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Chris Lattner3ed83c12007-12-09 21:11:08 +0000174 CurLine = LineNo;
Mike Stump11289f42009-09-09 15:08:12 +0000175
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000176 if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine)
Chris Lattner3ed83c12007-12-09 21:11:08 +0000177 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner068529a2008-08-17 03:12:02 +0000179 OS << '\n';
Chris Lattner3ed83c12007-12-09 21:11:08 +0000180 EmittedTokensOnThisLine = false;
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000181 EmittedMacroOnThisLine = false;
Chris Lattner3ed83c12007-12-09 21:11:08 +0000182 return true;
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000183 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000184
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000185 // If this line is "close enough" to the original line, just print newlines,
186 // otherwise print a #line directive.
Daniel Dunbare7781312008-09-26 01:13:35 +0000187 if (LineNo-CurLine <= 8) {
Chris Lattner5f075822007-07-23 05:14:05 +0000188 if (LineNo-CurLine == 1)
Chris Lattner068529a2008-08-17 03:12:02 +0000189 OS << '\n';
Chris Lattner3ed83c12007-12-09 21:11:08 +0000190 else if (LineNo == CurLine)
Chris Lattner8a425862009-01-16 07:36:28 +0000191 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner5f075822007-07-23 05:14:05 +0000192 else {
193 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattner068529a2008-08-17 03:12:02 +0000194 OS.write(NewLines, LineNo-CurLine);
Chris Lattner5f075822007-07-23 05:14:05 +0000195 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000196 } else {
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000197 WriteLineInfo(LineNo, 0, 0);
Mike Stump11289f42009-09-09 15:08:12 +0000198 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000199
Mike Stump11289f42009-09-09 15:08:12 +0000200 CurLine = LineNo;
Chris Lattner3ed83c12007-12-09 21:11:08 +0000201 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000202}
203
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000204
205/// FileChanged - Whenever the preprocessor enters or exits a #include file
206/// it invokes this handler. Update our conception of the current source
207/// position.
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000208void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
209 FileChangeReason Reason,
Chris Lattner66a740e2008-10-27 01:19:25 +0000210 SrcMgr::CharacteristicKind NewFileType) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000211 // Unless we are exiting a #include, make sure to skip ahead to the line the
212 // #include directive was at.
Chris Lattner87f267e2006-11-21 05:02:33 +0000213 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000214 if (Reason == PPCallbacks::EnterFile) {
Chris Lattnerd8cc8842009-01-30 18:44:17 +0000215 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
216 if (IncludeLoc.isValid())
217 MoveToLine(IncludeLoc);
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000218 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000219 MoveToLine(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000220
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000221 // TODO GCC emits the # directive for this directive on the line AFTER the
222 // directive and emits a bunch of spaces that aren't needed. Emulate this
223 // strange behavior.
224 }
Mike Stump11289f42009-09-09 15:08:12 +0000225
Chris Lattner8a425862009-01-16 07:36:28 +0000226 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner839150e2009-03-27 17:13:49 +0000227 // FIXME: Should use presumed line #!
Chris Lattner88ea93e2009-02-04 01:06:56 +0000228 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000229
Chris Lattner3ed83c12007-12-09 21:11:08 +0000230 if (DisableLineMarkers) return;
231
Chris Lattner4c4a2452007-07-24 06:57:14 +0000232 CurFilename.clear();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000233 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattner4c4a2452007-07-24 06:57:14 +0000234 Lexer::Stringify(CurFilename);
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000235 FileType = NewFileType;
236
237 if (!Initialized) {
238 WriteLineInfo(CurLine);
239 Initialized = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000240 }
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000241
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000242 switch (Reason) {
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000243 case PPCallbacks::EnterFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000244 WriteLineInfo(CurLine, " 1", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000245 break;
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000246 case PPCallbacks::ExitFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000247 WriteLineInfo(CurLine, " 2", 2);
Chris Lattner3338ba82006-07-04 21:19:39 +0000248 break;
Mike Stump11289f42009-09-09 15:08:12 +0000249 case PPCallbacks::SystemHeaderPragma:
250 case PPCallbacks::RenameFile:
Daniel Dunbar98e0e532008-09-05 03:22:57 +0000251 WriteLineInfo(CurLine);
252 break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000253 }
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000254}
255
Chris Lattner5eef5072009-01-16 19:25:54 +0000256/// Ident - Handle #ident directives when read by the preprocessor.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000257///
Chris Lattnerb8d6d5a2006-11-21 04:09:30 +0000258void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000259 MoveToLine(Loc);
Mike Stump11289f42009-09-09 15:08:12 +0000260
Chris Lattner068529a2008-08-17 03:12:02 +0000261 OS.write("#ident ", strlen("#ident "));
262 OS.write(&S[0], S.size());
Chris Lattner87f267e2006-11-21 05:02:33 +0000263 EmittedTokensOnThisLine = true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000264}
265
Chris Lattnercac63f32009-04-12 01:56:53 +0000266/// MacroDefined - This hook is called whenever a macro definition is seen.
267void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
268 const MacroInfo *MI) {
269 // Only print out macro definitions in -dD mode.
270 if (!DumpDefines ||
271 // Ignore __FILE__ etc.
272 MI->isBuiltinMacro()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000273
Chris Lattnercac63f32009-04-12 01:56:53 +0000274 MoveToLine(MI->getDefinitionLoc());
275 PrintMacroDefinition(*II, *MI, PP, OS);
Eli Friedmanfd80b2a2009-06-02 07:55:39 +0000276 EmittedMacroOnThisLine = true;
Chris Lattnercac63f32009-04-12 01:56:53 +0000277}
278
279
Chris Lattner5eef5072009-01-16 19:25:54 +0000280void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
Mike Stump11289f42009-09-09 15:08:12 +0000281 const IdentifierInfo *Kind,
Chris Lattner5eef5072009-01-16 19:25:54 +0000282 const std::string &Str) {
283 MoveToLine(Loc);
284 OS << "#pragma comment(" << Kind->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000285
Chris Lattner5eef5072009-01-16 19:25:54 +0000286 if (!Str.empty()) {
287 OS << ", \"";
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattner5eef5072009-01-16 19:25:54 +0000289 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
290 unsigned char Char = Str[i];
Chris Lattnerdde6eb32009-01-16 22:13:37 +0000291 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattner5eef5072009-01-16 19:25:54 +0000292 OS << (char)Char;
293 else // Output anything hard as an octal escape.
294 OS << '\\'
295 << (char)('0'+ ((Char >> 6) & 7))
296 << (char)('0'+ ((Char >> 3) & 7))
297 << (char)('0'+ ((Char >> 0) & 7));
298 }
299 OS << '"';
300 }
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner5eef5072009-01-16 19:25:54 +0000302 OS << ')';
303 EmittedTokensOnThisLine = true;
304}
305
306
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000307/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner3ed83c12007-12-09 21:11:08 +0000308/// is called for the first token on each new line. If this really is the start
309/// of a new logical line, handle it and return true, otherwise return false.
310/// This may not be the start of a logical line because the "start of line"
Chris Lattner8a425862009-01-16 07:36:28 +0000311/// marker is set for spelling lines, not instantiation ones.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000312bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000313 // Figure out what line we went to and insert the appropriate number of
314 // newline characters.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000315 if (!MoveToLine(Tok.getLocation()))
316 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000318 // Print out space characters so that the first token on a line is
319 // indented for easy reading.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000320 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattner8a425862009-01-16 07:36:28 +0000321 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000322
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000323 // This hack prevents stuff like:
324 // #define HASH #
325 // HASH define foo bar
326 // From having the # character end up at column 1, which makes it so it
327 // is not handled as a #define next time through the preprocessor if in
328 // -fpreprocessed mode.
Chris Lattner3c69f122007-10-09 18:03:42 +0000329 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattner068529a2008-08-17 03:12:02 +0000330 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000331
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000332 // Otherwise, indent the appropriate number of spaces.
333 for (; ColNo > 1; --ColNo)
Chris Lattner068529a2008-08-17 03:12:02 +0000334 OS << ' ';
Mike Stump11289f42009-09-09 15:08:12 +0000335
Chris Lattner3ed83c12007-12-09 21:11:08 +0000336 return true;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000337}
338
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000339void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
340 unsigned Len) {
341 unsigned NumNewlines = 0;
342 for (; Len; --Len, ++TokStr) {
343 if (*TokStr != '\n' &&
344 *TokStr != '\r')
345 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000346
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000347 ++NumNewlines;
Mike Stump11289f42009-09-09 15:08:12 +0000348
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000349 // If we have \n\r or \r\n, skip both and count as one line.
350 if (Len != 1 &&
351 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
352 TokStr[0] != TokStr[1])
353 ++TokStr, --Len;
354 }
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000356 if (NumNewlines == 0) return;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Chris Lattnera5e67752009-06-15 04:08:28 +0000358 CurLine += NumNewlines;
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000359}
360
361
Chris Lattner5de858c2006-07-04 19:04:44 +0000362namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000363struct UnknownPragmaHandler : public PragmaHandler {
364 const char *Prefix;
Chris Lattner87f267e2006-11-21 05:02:33 +0000365 PrintPPOutputPPCallbacks *Callbacks;
Mike Stump11289f42009-09-09 15:08:12 +0000366
Chris Lattner87f267e2006-11-21 05:02:33 +0000367 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
368 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattner146762e2007-07-20 16:59:19 +0000369 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000370 // Figure out what line we went to and insert the appropriate number of
371 // newline characters.
Chris Lattner87f267e2006-11-21 05:02:33 +0000372 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattner068529a2008-08-17 03:12:02 +0000373 Callbacks->OS.write(Prefix, strlen(Prefix));
Mike Stump11289f42009-09-09 15:08:12 +0000374
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000375 // Read and print all of the pragma tokens.
Chris Lattner3c69f122007-10-09 18:03:42 +0000376 while (PragmaTok.isNot(tok::eom)) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000377 if (PragmaTok.hasLeadingSpace())
Chris Lattner068529a2008-08-17 03:12:02 +0000378 Callbacks->OS << ' ';
Chris Lattnerf46be6c2006-07-04 22:19:33 +0000379 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattner068529a2008-08-17 03:12:02 +0000380 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000381 PP.LexUnexpandedToken(PragmaTok);
382 }
Chris Lattner068529a2008-08-17 03:12:02 +0000383 Callbacks->OS << '\n';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000384 }
385};
Chris Lattner5de858c2006-07-04 19:04:44 +0000386} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000387
Chris Lattner4418ce12007-07-23 06:09:34 +0000388
Chris Lattnerfc001b02009-02-06 05:56:11 +0000389static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
390 PrintPPOutputPPCallbacks *Callbacks,
391 llvm::raw_ostream &OS) {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000392 char Buffer[256];
Chris Lattnerfc001b02009-02-06 05:56:11 +0000393 Token PrevTok;
Chris Lattner3ff2e692007-10-10 20:45:16 +0000394 while (1) {
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattner67c38482006-07-04 23:24:26 +0000396 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner3ed83c12007-12-09 21:11:08 +0000397 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
398 // done.
Mike Stump11289f42009-09-09 15:08:12 +0000399 } else if (Tok.hasLeadingSpace() ||
Chris Lattner4418ce12007-07-23 06:09:34 +0000400 // If we haven't emitted a token on this line yet, PrevTok isn't
401 // useful to look at and no concatenation could happen anyway.
Chris Lattnerd63c8a52007-07-23 23:21:34 +0000402 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattner4418ce12007-07-23 06:09:34 +0000403 // Don't print "-" next to "-", it would form "--".
404 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattner068529a2008-08-17 03:12:02 +0000405 OS << ' ';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Chris Lattner0af98232007-07-23 06:14:36 +0000408 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000409 OS << II->getName();
Chris Lattner9892ea22009-01-26 19:33:54 +0000410 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
411 Tok.getLiteralData()) {
412 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner0af98232007-07-23 06:14:36 +0000413 } else if (Tok.getLength() < 256) {
Chris Lattneref9eae12006-07-04 22:33:12 +0000414 const char *TokPtr = Buffer;
415 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattner068529a2008-08-17 03:12:02 +0000416 OS.write(TokPtr, Len);
Mike Stump11289f42009-09-09 15:08:12 +0000417
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000418 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump11289f42009-09-09 15:08:12 +0000419 // line number.
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000420 if (Tok.getKind() == tok::comment)
421 Callbacks->HandleNewlinesInToken(TokPtr, Len);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000422 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000423 std::string S = PP.getSpelling(Tok);
Chris Lattner068529a2008-08-17 03:12:02 +0000424 OS.write(&S[0], S.size());
Mike Stump11289f42009-09-09 15:08:12 +0000425
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000426 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump11289f42009-09-09 15:08:12 +0000427 // line number.
Chris Lattnerf2d49da92009-06-15 01:25:23 +0000428 if (Tok.getKind() == tok::comment)
429 Callbacks->HandleNewlinesInToken(&S[0], S.size());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000430 }
Chris Lattner87f267e2006-11-21 05:02:33 +0000431 Callbacks->SetEmittedTokensOnThisLine();
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattner3ff2e692007-10-10 20:45:16 +0000433 if (Tok.is(tok::eof)) break;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Chris Lattner3ff2e692007-10-10 20:45:16 +0000435 PrevTok = Tok;
436 PP.Lex(Tok);
437 }
Chris Lattnerfc001b02009-02-06 05:56:11 +0000438}
439
Chris Lattner1ec246d2009-02-10 22:28:19 +0000440namespace {
441 struct SortMacrosByID {
442 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
443 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000444 return LHS.first->getName() < RHS.first->getName();
Chris Lattner1ec246d2009-02-10 22:28:19 +0000445 }
446 };
447}
Chris Lattnerfc001b02009-02-06 05:56:11 +0000448
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000449static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
Eli Friedman26c672b2009-05-19 01:32:34 +0000450 // -dM mode just scans and ignores all tokens in the files, then dumps out
451 // the macro table at the end.
452 PP.EnterMainSourceFile();
453
454 Token Tok;
455 do PP.Lex(Tok);
456 while (Tok.isNot(tok::eof));
457
458 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
459 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
460 I != E; ++I)
461 MacrosByID.push_back(*I);
462 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
463
464 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
465 MacroInfo &MI = *MacrosByID[i].second;
Mike Stump11289f42009-09-09 15:08:12 +0000466 // Ignore computed macros like __LINE__ and friends.
Eli Friedman26c672b2009-05-19 01:32:34 +0000467 if (MI.isBuiltinMacro()) continue;
468
469 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
470 *OS << "\n";
471 }
472}
473
Chris Lattnerfc001b02009-02-06 05:56:11 +0000474/// DoPrintPreprocessedInput - This implements -E mode.
475///
Eli Friedman6af494b2009-05-19 03:06:47 +0000476void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
Daniel Dunbar22bdabf2009-11-11 10:07:44 +0000477 const PreprocessorOutputOptions &Opts) {
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000478 // Show macros with no output is handled specially.
479 if (!Opts.ShowCPP) {
480 assert(Opts.ShowMacros && "Not yet implemented!");
481 DoPrintMacros(PP, OS);
482 return;
483 }
484
Chris Lattnerfc001b02009-02-06 05:56:11 +0000485 // Inform the preprocessor whether we want it to retain comments or not, due
486 // to -C or -CC.
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000487 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
Eli Friedmanfcb57d52009-05-19 01:02:07 +0000488
489 OS->SetBufferSize(64*1024);
Chris Lattnercac63f32009-04-12 01:56:53 +0000490
Eli Friedman6af494b2009-05-19 03:06:47 +0000491 PrintPPOutputPPCallbacks *Callbacks =
Daniel Dunbar531f6c62009-11-11 10:07:22 +0000492 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
493 Opts.ShowMacros);
Eli Friedman26c672b2009-05-19 01:32:34 +0000494 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
495 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
496 Callbacks));
Chris Lattnerfc001b02009-02-06 05:56:11 +0000497
Eli Friedman26c672b2009-05-19 01:32:34 +0000498 PP.setPPCallbacks(Callbacks);
Chris Lattnerfc001b02009-02-06 05:56:11 +0000499
Eli Friedman26c672b2009-05-19 01:32:34 +0000500 // After we have configured the preprocessor, enter the main file.
501 PP.EnterMainSourceFile();
Chris Lattnerfc001b02009-02-06 05:56:11 +0000502
Eli Friedman26c672b2009-05-19 01:32:34 +0000503 // Consume all of the tokens that come from the predefines buffer. Those
504 // should not be emitted into the output and are guaranteed to be at the
505 // start.
506 const SourceManager &SourceMgr = PP.getSourceManager();
507 Token Tok;
508 do PP.Lex(Tok);
509 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
510 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
511 "<built-in>"));
Chris Lattnerfc001b02009-02-06 05:56:11 +0000512
Eli Friedman26c672b2009-05-19 01:32:34 +0000513 // Read all the preprocessed tokens, printing them out to the stream.
514 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
515 *OS << '\n';
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000516}
517