blob: b1a1936150302cd2fc57cb22cc42ceb48931c041 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This code simply runs the preprocessor on the input file and prints out the
11// result. This is the traditional behavior of the -E option.
12//
13//===----------------------------------------------------------------------===//
14
Eli Friedmanb09f6e12009-05-19 04:14:29 +000015#include "clang/Frontend/Utils.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000016#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Frontend/PreprocessorOutputOptions.h"
Chris Lattnerf73903a2009-02-06 06:45:26 +000019#include "clang/Lex/MacroInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Lex/PPCallbacks.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Lex/Pragma.h"
Daniel Dunbar775bee72009-11-11 10:07:22 +000022#include "clang/Lex/Preprocessor.h"
Chris Lattnerd7038e12009-02-13 00:46:04 +000023#include "clang/Lex/TokenConcatenation.h"
Chris Lattnerd8e30832007-07-24 06:57:14 +000024#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/Config/config.h"
Chris Lattnerdceb6a72008-08-17 01:47:12 +000027#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028#include <cstdio>
29using namespace clang;
30
Chris Lattnerd82df3a2009-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 Stump1eb44332009-09-09 15:08:12 +000036
Chris Lattnerd82df3a2009-04-12 01:56:53 +000037 if (MI.isFunctionLike()) {
38 OS << '(';
39 if (MI.arg_empty())
40 ;
Mike Stump1eb44332009-09-09 15:08:12 +000041 else if (MI.getNumArgs() == 1)
Chris Lattnerd82df3a2009-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 Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnerd82df3a2009-04-12 01:56:53 +000050 if (MI.isVariadic()) {
51 if (!MI.arg_empty())
52 OS << ',';
53 OS << "...";
54 }
55 OS << ')';
56 }
Mike Stump1eb44332009-09-09 15:08:12 +000057
Chris Lattnerd82df3a2009-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 Stump1eb44332009-09-09 15:08:12 +000062
Chris Lattnerd82df3a2009-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 Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattnerd82df3a2009-04-12 01:56:53 +000069 // Make sure we have enough space in the spelling buffer.
Ted Kremenek3e27c012009-11-03 06:18:05 +000070 if (I->getLength() > SpellingBuffer.size())
Chris Lattnerd82df3a2009-04-12 01:56:53 +000071 SpellingBuffer.resize(I->getLength());
Jay Foadbeaaccd2009-05-21 09:52:38 +000072 const char *Buffer = SpellingBuffer.data();
Chris Lattnerd82df3a2009-04-12 01:56:53 +000073 unsigned SpellingLen = PP.getSpelling(*I, Buffer);
74 OS.write(Buffer, SpellingLen);
75 }
76}
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078//===----------------------------------------------------------------------===//
79// Preprocessed token printer
80//===----------------------------------------------------------------------===//
81
Reid Spencer5f016e22007-07-11 17:01:13 +000082namespace {
83class PrintPPOutputPPCallbacks : public PPCallbacks {
84 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +000085 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +000086public:
87 llvm::raw_ostream &OS;
88private:
Reid Spencer5f016e22007-07-11 17:01:13 +000089 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +000090 bool EmittedTokensOnThisLine;
Eli Friedman3e753e22009-06-02 07:55:39 +000091 bool EmittedMacroOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +000092 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +000093 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +000094 bool Initialized;
Eli Friedman12d3b1d2009-05-19 03:06:47 +000095 bool DisableLineMarkers;
96 bool DumpDefines;
Reid Spencer5f016e22007-07-11 17:01:13 +000097public:
Eli Friedman12d3b1d2009-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) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000103 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 EmittedTokensOnThisLine = false;
Eli Friedman3e753e22009-06-02 07:55:39 +0000105 EmittedMacroOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000106 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000107 Initialized = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000111 bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000114 SrcMgr::CharacteristicKind FileType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 virtual void Ident(SourceLocation Loc, const std::string &str);
Mike Stump1eb44332009-09-09 15:08:12 +0000116 virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000117 const std::string &Str);
118
Reid Spencer5f016e22007-07-11 17:01:13 +0000119
Chris Lattner5f180322007-12-09 21:11:08 +0000120 bool HandleFirstTokOnLine(Token &Tok);
121 bool MoveToLine(SourceLocation Loc);
Chris Lattnerd7038e12009-02-13 00:46:04 +0000122 bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
123 return ConcatInfo.AvoidConcat(PrevTok, Tok);
124 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000125 void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner3ee211f2009-06-15 01:25:23 +0000127 void HandleNewlinesInToken(const char *TokStr, unsigned Len);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Chris Lattnerd82df3a2009-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 Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132};
Chris Lattner5db17c92008-04-08 04:16:20 +0000133} // end anonymous namespace
Reid Spencer5f016e22007-07-11 17:01:13 +0000134
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000135void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
136 const char *Extra,
137 unsigned ExtraLen) {
Eli Friedman3e753e22009-06-02 07:55:39 +0000138 if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000139 OS << '\n';
140 EmittedTokensOnThisLine = false;
Eli Friedman3e753e22009-06-02 07:55:39 +0000141 EmittedMacroOnThisLine = false;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000142 }
143
144 OS << '#' << ' ' << LineNo << ' ' << '"';
145 OS.write(&CurFilename[0], CurFilename.size());
146 OS << '"';
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000148 if (ExtraLen)
149 OS.write(Extra, ExtraLen);
150
Chris Lattner0b9e7362008-09-26 21:18:42 +0000151 if (FileType == SrcMgr::C_System)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000152 OS.write(" 3", 2);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000153 else if (FileType == SrcMgr::C_ExternCSystem)
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000154 OS.write(" 3 4", 4);
155 OS << '\n';
156}
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158/// MoveToLine - Move the output to the source line specified by the location
159/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner5f180322007-12-09 21:11:08 +0000160/// #line directive. This returns false if already at the specified line, true
161/// if some newlines were emitted.
162bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000163 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000166 if (LineNo == CurLine) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner5f180322007-12-09 21:11:08 +0000168 CurLine = LineNo;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Eli Friedman3e753e22009-06-02 07:55:39 +0000170 if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine)
Chris Lattner5f180322007-12-09 21:11:08 +0000171 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattnere96de3e2008-08-17 03:12:02 +0000173 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000174 EmittedTokensOnThisLine = false;
Eli Friedman3e753e22009-06-02 07:55:39 +0000175 EmittedMacroOnThisLine = false;
Chris Lattner5f180322007-12-09 21:11:08 +0000176 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000178
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 // If this line is "close enough" to the original line, just print newlines,
180 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000181 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000182 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000183 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000184 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000185 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000186 else {
187 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000188 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000189 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000191 WriteLineInfo(LineNo, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000192 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000193
Mike Stump1eb44332009-09-09 15:08:12 +0000194 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000195 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000196}
197
198
199/// FileChanged - Whenever the preprocessor enters or exits a #include file
200/// it invokes this handler. Update our conception of the current source
201/// position.
202void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
203 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000204 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // Unless we are exiting a #include, make sure to skip ahead to the line the
206 // #include directive was at.
207 SourceManager &SourceMgr = PP.getSourceManager();
208 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000209 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
210 if (IncludeLoc.isValid())
211 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
213 MoveToLine(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 // TODO GCC emits the # directive for this directive on the line AFTER the
216 // directive and emits a bunch of spaces that aren't needed. Emulate this
217 // strange behavior.
218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000220 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000221 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000222 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000223
Chris Lattner5f180322007-12-09 21:11:08 +0000224 if (DisableLineMarkers) return;
225
Chris Lattnerd8e30832007-07-24 06:57:14 +0000226 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000227 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000228 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000229 FileType = NewFileType;
230
231 if (!Initialized) {
232 WriteLineInfo(CurLine);
233 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 switch (Reason) {
237 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000238 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 break;
240 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000241 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000243 case PPCallbacks::SystemHeaderPragma:
244 case PPCallbacks::RenameFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000245 WriteLineInfo(CurLine);
246 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000248}
249
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000250/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000251///
252void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
253 MoveToLine(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chris Lattnere96de3e2008-08-17 03:12:02 +0000255 OS.write("#ident ", strlen("#ident "));
256 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 EmittedTokensOnThisLine = true;
258}
259
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000260/// MacroDefined - This hook is called whenever a macro definition is seen.
261void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
262 const MacroInfo *MI) {
263 // Only print out macro definitions in -dD mode.
264 if (!DumpDefines ||
265 // Ignore __FILE__ etc.
266 MI->isBuiltinMacro()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000268 MoveToLine(MI->getDefinitionLoc());
269 PrintMacroDefinition(*II, *MI, PP, OS);
Eli Friedman3e753e22009-06-02 07:55:39 +0000270 EmittedMacroOnThisLine = true;
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000271}
272
273
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000274void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000275 const IdentifierInfo *Kind,
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000276 const std::string &Str) {
277 MoveToLine(Loc);
278 OS << "#pragma comment(" << Kind->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000280 if (!Str.empty()) {
281 OS << ", \"";
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000283 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
284 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000285 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000286 OS << (char)Char;
287 else // Output anything hard as an octal escape.
288 OS << '\\'
289 << (char)('0'+ ((Char >> 6) & 7))
290 << (char)('0'+ ((Char >> 3) & 7))
291 << (char)('0'+ ((Char >> 0) & 7));
292 }
293 OS << '"';
294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000296 OS << ')';
297 EmittedTokensOnThisLine = true;
298}
299
300
Reid Spencer5f016e22007-07-11 17:01:13 +0000301/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000302/// is called for the first token on each new line. If this really is the start
303/// of a new logical line, handle it and return true, otherwise return false.
304/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000305/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000306bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 // Figure out what line we went to and insert the appropriate number of
308 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000309 if (!MoveToLine(Tok.getLocation()))
310 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // Print out space characters so that the first token on a line is
313 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000314 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000315 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 // This hack prevents stuff like:
318 // #define HASH #
319 // HASH define foo bar
320 // From having the # character end up at column 1, which makes it so it
321 // is not handled as a #define next time through the preprocessor if in
322 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000323 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000324 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326 // Otherwise, indent the appropriate number of spaces.
327 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000328 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Chris Lattner5f180322007-12-09 21:11:08 +0000330 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000331}
332
Chris Lattner3ee211f2009-06-15 01:25:23 +0000333void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
334 unsigned Len) {
335 unsigned NumNewlines = 0;
336 for (; Len; --Len, ++TokStr) {
337 if (*TokStr != '\n' &&
338 *TokStr != '\r')
339 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner3ee211f2009-06-15 01:25:23 +0000341 ++NumNewlines;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner3ee211f2009-06-15 01:25:23 +0000343 // If we have \n\r or \r\n, skip both and count as one line.
344 if (Len != 1 &&
345 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
346 TokStr[0] != TokStr[1])
347 ++TokStr, --Len;
348 }
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner3ee211f2009-06-15 01:25:23 +0000350 if (NumNewlines == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnered2d7c42009-06-15 04:08:28 +0000352 CurLine += NumNewlines;
Chris Lattner3ee211f2009-06-15 01:25:23 +0000353}
354
355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356namespace {
357struct UnknownPragmaHandler : public PragmaHandler {
358 const char *Prefix;
359 PrintPPOutputPPCallbacks *Callbacks;
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
362 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000363 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 // Figure out what line we went to and insert the appropriate number of
365 // newline characters.
366 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000367 Callbacks->OS.write(Prefix, strlen(Prefix));
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000370 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000372 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000374 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 PP.LexUnexpandedToken(PragmaTok);
376 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000377 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 }
379};
380} // end anonymous namespace
381
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000382
Chris Lattner59076ab2009-02-06 05:56:11 +0000383static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
384 PrintPPOutputPPCallbacks *Callbacks,
385 llvm::raw_ostream &OS) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000387 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000388 while (1) {
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000391 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
392 // done.
Mike Stump1eb44332009-09-09 15:08:12 +0000393 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000394 // If we haven't emitted a token on this line yet, PrevTok isn't
395 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000396 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000397 // Don't print "-" next to "-", it would form "--".
398 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000399 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Chris Lattner2933f412007-07-23 06:14:36 +0000402 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000403 OS << II->getName();
Chris Lattner33116d62009-01-26 19:33:54 +0000404 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
405 Tok.getLiteralData()) {
406 OS.write(Tok.getLiteralData(), Tok.getLength());
Chris Lattner2933f412007-07-23 06:14:36 +0000407 } else if (Tok.getLength() < 256) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 const char *TokPtr = Buffer;
409 unsigned Len = PP.getSpelling(Tok, TokPtr);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000410 OS.write(TokPtr, Len);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner3ee211f2009-06-15 01:25:23 +0000412 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump1eb44332009-09-09 15:08:12 +0000413 // line number.
Chris Lattner3ee211f2009-06-15 01:25:23 +0000414 if (Tok.getKind() == tok::comment)
415 Callbacks->HandleNewlinesInToken(TokPtr, Len);
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 } else {
417 std::string S = PP.getSpelling(Tok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000418 OS.write(&S[0], S.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner3ee211f2009-06-15 01:25:23 +0000420 // Tokens that can contain embedded newlines need to adjust our current
Mike Stump1eb44332009-09-09 15:08:12 +0000421 // line number.
Chris Lattner3ee211f2009-06-15 01:25:23 +0000422 if (Tok.getKind() == tok::comment)
423 Callbacks->HandleNewlinesInToken(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 }
425 Callbacks->SetEmittedTokensOnThisLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner6f688e12007-10-10 20:45:16 +0000427 if (Tok.is(tok::eof)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattner6f688e12007-10-10 20:45:16 +0000429 PrevTok = Tok;
430 PP.Lex(Tok);
431 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000432}
433
Chris Lattner2a2bb182009-02-10 22:28:19 +0000434namespace {
435 struct SortMacrosByID {
436 typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
437 bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
Daniel Dunbar01eb9b92009-10-18 21:17:35 +0000438 return LHS.first->getName() < RHS.first->getName();
Chris Lattner2a2bb182009-02-10 22:28:19 +0000439 }
440 };
441}
Chris Lattner59076ab2009-02-06 05:56:11 +0000442
Daniel Dunbar775bee72009-11-11 10:07:22 +0000443static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
Eli Friedmanf1db5852009-05-19 01:32:34 +0000444 // -dM mode just scans and ignores all tokens in the files, then dumps out
445 // the macro table at the end.
446 PP.EnterMainSourceFile();
447
448 Token Tok;
449 do PP.Lex(Tok);
450 while (Tok.isNot(tok::eof));
451
452 std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
453 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
454 I != E; ++I)
455 MacrosByID.push_back(*I);
456 std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
457
458 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
459 MacroInfo &MI = *MacrosByID[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000460 // Ignore computed macros like __LINE__ and friends.
Eli Friedmanf1db5852009-05-19 01:32:34 +0000461 if (MI.isBuiltinMacro()) continue;
462
463 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
464 *OS << "\n";
465 }
466}
467
Chris Lattner59076ab2009-02-06 05:56:11 +0000468/// DoPrintPreprocessedInput - This implements -E mode.
469///
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000470void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
Daniel Dunbar775bee72009-11-11 10:07:22 +0000471 PreprocessorOutputOptions &Opts) {
472 // Show macros with no output is handled specially.
473 if (!Opts.ShowCPP) {
474 assert(Opts.ShowMacros && "Not yet implemented!");
475 DoPrintMacros(PP, OS);
476 return;
477 }
478
Chris Lattner59076ab2009-02-06 05:56:11 +0000479 // Inform the preprocessor whether we want it to retain comments or not, due
480 // to -C or -CC.
Daniel Dunbar775bee72009-11-11 10:07:22 +0000481 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000482
483 OS->SetBufferSize(64*1024);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000484
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000485 PrintPPOutputPPCallbacks *Callbacks =
Daniel Dunbar775bee72009-11-11 10:07:22 +0000486 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
487 Opts.ShowMacros);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000488 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
489 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
490 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000491
Eli Friedmanf1db5852009-05-19 01:32:34 +0000492 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000493
Eli Friedmanf1db5852009-05-19 01:32:34 +0000494 // After we have configured the preprocessor, enter the main file.
495 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000496
Eli Friedmanf1db5852009-05-19 01:32:34 +0000497 // Consume all of the tokens that come from the predefines buffer. Those
498 // should not be emitted into the output and are guaranteed to be at the
499 // start.
500 const SourceManager &SourceMgr = PP.getSourceManager();
501 Token Tok;
502 do PP.Lex(Tok);
503 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
504 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
505 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000506
Eli Friedmanf1db5852009-05-19 01:32:34 +0000507 // Read all the preprocessed tokens, printing them out to the stream.
508 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
509 *OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000510}
511