blob: 774372c86934e344896408cc163d57d7e31eeb92 [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"
Benjamin Kramer3ff2a4b2010-01-19 17:42:20 +000025#include "llvm/ADT/STLExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026#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 << '(';
Chris Lattner13d55582009-12-09 02:08:14 +000039 if (!MI.arg_empty()) {
Chris Lattnerd82df3a2009-04-12 01:56:53 +000040 MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
Chris Lattner13d55582009-12-09 02:08:14 +000041 for (; AI+1 != E; ++AI) {
42 OS << (*AI)->getName();
Chris Lattner807b93e2009-12-07 01:58:34 +000043 OS << ',';
Chris Lattner807b93e2009-12-07 01:58:34 +000044 }
Chris Lattner13d55582009-12-09 02:08:14 +000045
46 // Last argument.
47 if ((*AI)->getName() == "__VA_ARGS__")
48 OS << "...";
49 else
50 OS << (*AI)->getName();
Chris Lattnerd82df3a2009-04-12 01:56:53 +000051 }
Mike Stump1eb44332009-09-09 15:08:12 +000052
Chris Lattner807b93e2009-12-07 01:58:34 +000053 if (MI.isGNUVarargs())
54 OS << "..."; // #define foo(x...)
55
Chris Lattnerd82df3a2009-04-12 01:56:53 +000056 OS << ')';
57 }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Chris Lattnerd82df3a2009-04-12 01:56:53 +000059 // GCC always emits a space, even if the macro body is empty. However, do not
60 // want to emit two spaces if the first token has a leading space.
61 if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
62 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattnerd82df3a2009-04-12 01:56:53 +000064 llvm::SmallVector<char, 128> SpellingBuffer;
65 for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
66 I != E; ++I) {
67 if (I->hasLeadingSpace())
68 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +000069
Benjamin Kramerddeea562010-02-27 13:44:12 +000070 OS << PP.getSpelling(*I, SpellingBuffer);
Chris Lattnerd82df3a2009-04-12 01:56:53 +000071 }
72}
73
Reid Spencer5f016e22007-07-11 17:01:13 +000074//===----------------------------------------------------------------------===//
75// Preprocessed token printer
76//===----------------------------------------------------------------------===//
77
Reid Spencer5f016e22007-07-11 17:01:13 +000078namespace {
79class PrintPPOutputPPCallbacks : public PPCallbacks {
80 Preprocessor &PP;
Chris Lattnerd7038e12009-02-13 00:46:04 +000081 TokenConcatenation ConcatInfo;
Chris Lattnere96de3e2008-08-17 03:12:02 +000082public:
83 llvm::raw_ostream &OS;
84private:
Reid Spencer5f016e22007-07-11 17:01:13 +000085 unsigned CurLine;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 bool EmittedTokensOnThisLine;
Eli Friedman3e753e22009-06-02 07:55:39 +000087 bool EmittedMacroOnThisLine;
Chris Lattner9d728512008-10-27 01:19:25 +000088 SrcMgr::CharacteristicKind FileType;
Chris Lattnerd8e30832007-07-24 06:57:14 +000089 llvm::SmallString<512> CurFilename;
Daniel Dunbar737bdb42008-09-05 03:22:57 +000090 bool Initialized;
Eli Friedman12d3b1d2009-05-19 03:06:47 +000091 bool DisableLineMarkers;
92 bool DumpDefines;
Chris Lattnerf7449342009-12-07 01:42:56 +000093 bool UseLineDirective;
Reid Spencer5f016e22007-07-11 17:01:13 +000094public:
Eli Friedman12d3b1d2009-05-19 03:06:47 +000095 PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
96 bool lineMarkers, bool defines)
97 : PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
98 DumpDefines(defines) {
Reid Spencer5f016e22007-07-11 17:01:13 +000099 CurLine = 0;
Chris Lattnerd8e30832007-07-24 06:57:14 +0000100 CurFilename += "<uninit>";
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 EmittedTokensOnThisLine = false;
Eli Friedman3e753e22009-06-02 07:55:39 +0000102 EmittedMacroOnThisLine = false;
Chris Lattner0b9e7362008-09-26 21:18:42 +0000103 FileType = SrcMgr::C_User;
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000104 Initialized = false;
Chris Lattnerf7449342009-12-07 01:42:56 +0000105
106 // If we're in microsoft mode, use normal #line instead of line markers.
107 UseLineDirective = PP.getLangOptions().Microsoft;
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
Chris Lattnerf7449342009-12-07 01:42:56 +0000144 // Emit #line directives or GNU line markers depending on what mode we're in.
145 if (UseLineDirective) {
146 OS << "#line" << ' ' << LineNo << ' ' << '"';
147 OS.write(&CurFilename[0], CurFilename.size());
148 OS << '"';
149 } else {
150 OS << '#' << ' ' << LineNo << ' ' << '"';
151 OS.write(&CurFilename[0], CurFilename.size());
152 OS << '"';
153
Steve Naroffd76fbda2009-12-06 01:02:14 +0000154 if (ExtraLen)
155 OS.write(Extra, ExtraLen);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000156
Steve Naroffd76fbda2009-12-06 01:02:14 +0000157 if (FileType == SrcMgr::C_System)
158 OS.write(" 3", 2);
159 else if (FileType == SrcMgr::C_ExternCSystem)
160 OS.write(" 3 4", 4);
161 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000162 OS << '\n';
163}
164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165/// MoveToLine - Move the output to the source line specified by the location
166/// object. We can do this by emitting some number of \n's, or be emitting a
Chris Lattner5f180322007-12-09 21:11:08 +0000167/// #line directive. This returns false if already at the specified line, true
168/// if some newlines were emitted.
169bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000170 unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 if (DisableLineMarkers) {
Chris Lattner5f180322007-12-09 21:11:08 +0000173 if (LineNo == CurLine) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chris Lattner5f180322007-12-09 21:11:08 +0000175 CurLine = LineNo;
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Eli Friedman3e753e22009-06-02 07:55:39 +0000177 if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine)
Chris Lattner5f180322007-12-09 21:11:08 +0000178 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattnere96de3e2008-08-17 03:12:02 +0000180 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000181 EmittedTokensOnThisLine = false;
Eli Friedman3e753e22009-06-02 07:55:39 +0000182 EmittedMacroOnThisLine = false;
Chris Lattner5f180322007-12-09 21:11:08 +0000183 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // If this line is "close enough" to the original line, just print newlines,
187 // otherwise print a #line directive.
Daniel Dunbarfd966842008-09-26 01:13:35 +0000188 if (LineNo-CurLine <= 8) {
Chris Lattner822f9402007-07-23 05:14:05 +0000189 if (LineNo-CurLine == 1)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000190 OS << '\n';
Chris Lattner5f180322007-12-09 21:11:08 +0000191 else if (LineNo == CurLine)
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000192 return false; // Spelling line moved, but instantiation line didn't.
Chris Lattner822f9402007-07-23 05:14:05 +0000193 else {
194 const char *NewLines = "\n\n\n\n\n\n\n\n";
Chris Lattnere96de3e2008-08-17 03:12:02 +0000195 OS.write(NewLines, LineNo-CurLine);
Chris Lattner822f9402007-07-23 05:14:05 +0000196 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 } else {
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000198 WriteLineInfo(LineNo, 0, 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000199 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000200
Mike Stump1eb44332009-09-09 15:08:12 +0000201 CurLine = LineNo;
Chris Lattner5f180322007-12-09 21:11:08 +0000202 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000203}
204
205
206/// FileChanged - Whenever the preprocessor enters or exits a #include file
207/// it invokes this handler. Update our conception of the current source
208/// position.
209void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
210 FileChangeReason Reason,
Chris Lattner9d728512008-10-27 01:19:25 +0000211 SrcMgr::CharacteristicKind NewFileType) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 // Unless we are exiting a #include, make sure to skip ahead to the line the
213 // #include directive was at.
214 SourceManager &SourceMgr = PP.getSourceManager();
215 if (Reason == PPCallbacks::EnterFile) {
Chris Lattner71d8bfb2009-01-30 18:44:17 +0000216 SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
217 if (IncludeLoc.isValid())
218 MoveToLine(IncludeLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 } else if (Reason == PPCallbacks::SystemHeaderPragma) {
220 MoveToLine(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 // TODO GCC emits the # directive for this directive on the line AFTER the
223 // directive and emits a bunch of spaces that aren't needed. Emulate this
224 // strange behavior.
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000227 Loc = SourceMgr.getInstantiationLoc(Loc);
Chris Lattner16629382009-03-27 17:13:49 +0000228 // FIXME: Should use presumed line #!
Chris Lattner30fc9332009-02-04 01:06:56 +0000229 CurLine = SourceMgr.getInstantiationLineNumber(Loc);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000230
Chris Lattner5f180322007-12-09 21:11:08 +0000231 if (DisableLineMarkers) return;
232
Chris Lattnerd8e30832007-07-24 06:57:14 +0000233 CurFilename.clear();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000234 CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
Chris Lattnerd8e30832007-07-24 06:57:14 +0000235 Lexer::Stringify(CurFilename);
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000236 FileType = NewFileType;
237
238 if (!Initialized) {
239 WriteLineInfo(CurLine);
240 Initialized = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 }
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000242
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 switch (Reason) {
244 case PPCallbacks::EnterFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000245 WriteLineInfo(CurLine, " 1", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 break;
247 case PPCallbacks::ExitFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000248 WriteLineInfo(CurLine, " 2", 2);
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000250 case PPCallbacks::SystemHeaderPragma:
251 case PPCallbacks::RenameFile:
Daniel Dunbar737bdb42008-09-05 03:22:57 +0000252 WriteLineInfo(CurLine);
253 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000257/// Ident - Handle #ident directives when read by the preprocessor.
Reid Spencer5f016e22007-07-11 17:01:13 +0000258///
259void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
260 MoveToLine(Loc);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chris Lattnere96de3e2008-08-17 03:12:02 +0000262 OS.write("#ident ", strlen("#ident "));
263 OS.write(&S[0], S.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 EmittedTokensOnThisLine = true;
265}
266
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000267/// MacroDefined - This hook is called whenever a macro definition is seen.
268void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
269 const MacroInfo *MI) {
270 // Only print out macro definitions in -dD mode.
271 if (!DumpDefines ||
272 // Ignore __FILE__ etc.
273 MI->isBuiltinMacro()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000275 MoveToLine(MI->getDefinitionLoc());
276 PrintMacroDefinition(*II, *MI, PP, OS);
Eli Friedman3e753e22009-06-02 07:55:39 +0000277 EmittedMacroOnThisLine = true;
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000278}
279
280
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000281void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +0000282 const IdentifierInfo *Kind,
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000283 const std::string &Str) {
284 MoveToLine(Loc);
285 OS << "#pragma comment(" << Kind->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000287 if (!Str.empty()) {
288 OS << ", \"";
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000290 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
291 unsigned char Char = Str[i];
Chris Lattner52a3e9e2009-01-16 22:13:37 +0000292 if (isprint(Char) && Char != '\\' && Char != '"')
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000293 OS << (char)Char;
294 else // Output anything hard as an octal escape.
295 OS << '\\'
296 << (char)('0'+ ((Char >> 6) & 7))
297 << (char)('0'+ ((Char >> 3) & 7))
298 << (char)('0'+ ((Char >> 0) & 7));
299 }
300 OS << '"';
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Chris Lattnerc7d945d2009-01-16 19:25:54 +0000303 OS << ')';
304 EmittedTokensOnThisLine = true;
305}
306
307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
Chris Lattner5f180322007-12-09 21:11:08 +0000309/// is called for the first token on each new line. If this really is the start
310/// of a new logical line, handle it and return true, otherwise return false.
311/// This may not be the start of a logical line because the "start of line"
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000312/// marker is set for spelling lines, not instantiation ones.
Chris Lattner5f180322007-12-09 21:11:08 +0000313bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 // Figure out what line we went to and insert the appropriate number of
315 // newline characters.
Chris Lattner5f180322007-12-09 21:11:08 +0000316 if (!MoveToLine(Tok.getLocation()))
317 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319 // Print out space characters so that the first token on a line is
320 // indented for easy reading.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000321 const SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000322 unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 // This hack prevents stuff like:
325 // #define HASH #
326 // HASH define foo bar
327 // From having the # character end up at column 1, which makes it so it
328 // is not handled as a #define next time through the preprocessor if in
329 // -fpreprocessed mode.
Chris Lattner057aaf62007-10-09 18:03:42 +0000330 if (ColNo <= 1 && Tok.is(tok::hash))
Chris Lattnere96de3e2008-08-17 03:12:02 +0000331 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 // Otherwise, indent the appropriate number of spaces.
334 for (; ColNo > 1; --ColNo)
Chris Lattnere96de3e2008-08-17 03:12:02 +0000335 OS << ' ';
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner5f180322007-12-09 21:11:08 +0000337 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000338}
339
Chris Lattner3ee211f2009-06-15 01:25:23 +0000340void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
341 unsigned Len) {
342 unsigned NumNewlines = 0;
343 for (; Len; --Len, ++TokStr) {
344 if (*TokStr != '\n' &&
345 *TokStr != '\r')
346 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Chris Lattner3ee211f2009-06-15 01:25:23 +0000348 ++NumNewlines;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Chris Lattner3ee211f2009-06-15 01:25:23 +0000350 // If we have \n\r or \r\n, skip both and count as one line.
351 if (Len != 1 &&
352 (TokStr[1] == '\n' || TokStr[1] == '\r') &&
353 TokStr[0] != TokStr[1])
354 ++TokStr, --Len;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner3ee211f2009-06-15 01:25:23 +0000357 if (NumNewlines == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattnered2d7c42009-06-15 04:08:28 +0000359 CurLine += NumNewlines;
Chris Lattner3ee211f2009-06-15 01:25:23 +0000360}
361
362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363namespace {
364struct UnknownPragmaHandler : public PragmaHandler {
365 const char *Prefix;
366 PrintPPOutputPPCallbacks *Callbacks;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
369 : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
Chris Lattnerd2177732007-07-20 16:59:19 +0000370 virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 // Figure out what line we went to and insert the appropriate number of
372 // newline characters.
373 Callbacks->MoveToLine(PragmaTok.getLocation());
Chris Lattnere96de3e2008-08-17 03:12:02 +0000374 Callbacks->OS.write(Prefix, strlen(Prefix));
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // Read and print all of the pragma tokens.
Chris Lattner057aaf62007-10-09 18:03:42 +0000377 while (PragmaTok.isNot(tok::eom)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000378 if (PragmaTok.hasLeadingSpace())
Chris Lattnere96de3e2008-08-17 03:12:02 +0000379 Callbacks->OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 std::string TokSpell = PP.getSpelling(PragmaTok);
Chris Lattnere96de3e2008-08-17 03:12:02 +0000381 Callbacks->OS.write(&TokSpell[0], TokSpell.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000382 PP.LexUnexpandedToken(PragmaTok);
383 }
Chris Lattnere96de3e2008-08-17 03:12:02 +0000384 Callbacks->OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 }
386};
387} // end anonymous namespace
388
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000389
Chris Lattner59076ab2009-02-06 05:56:11 +0000390static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
391 PrintPPOutputPPCallbacks *Callbacks,
392 llvm::raw_ostream &OS) {
Benjamin Kramere3960072010-02-27 18:02:51 +0000393 char Buffer[256];
Chris Lattner59076ab2009-02-06 05:56:11 +0000394 Token PrevTok;
Chris Lattner6f688e12007-10-10 20:45:16 +0000395 while (1) {
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 // If this token is at the start of a line, emit newlines if needed.
Chris Lattner5f180322007-12-09 21:11:08 +0000398 if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
399 // done.
Mike Stump1eb44332009-09-09 15:08:12 +0000400 } else if (Tok.hasLeadingSpace() ||
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000401 // If we haven't emitted a token on this line yet, PrevTok isn't
402 // useful to look at and no concatenation could happen anyway.
Chris Lattnerb638a302007-07-23 23:21:34 +0000403 (Callbacks->hasEmittedTokensOnThisLine() &&
Chris Lattnerf0f2b292007-07-23 06:09:34 +0000404 // Don't print "-" next to "-", it would form "--".
405 Callbacks->AvoidConcat(PrevTok, Tok))) {
Chris Lattnere96de3e2008-08-17 03:12:02 +0000406 OS << ' ';
Reid Spencer5f016e22007-07-11 17:01:13 +0000407 }
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Benjamin Kramere3960072010-02-27 18:02:51 +0000409 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
410 OS << II->getName();
411 } else if (Tok.isLiteral() && !Tok.needsCleaning() &&
412 Tok.getLiteralData()) {
413 OS.write(Tok.getLiteralData(), Tok.getLength());
414 } else if (Tok.getLength() < 256) {
415 const char *TokPtr = Buffer;
416 unsigned Len = PP.getSpelling(Tok, TokPtr);
417 OS.write(TokPtr, Len);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Benjamin Kramere3960072010-02-27 18:02:51 +0000419 // Tokens that can contain embedded newlines need to adjust our current
420 // line number.
421 if (Tok.getKind() == tok::comment)
422 Callbacks->HandleNewlinesInToken(TokPtr, Len);
423 } else {
424 std::string S = PP.getSpelling(Tok);
425 OS.write(&S[0], S.size());
426
427 // Tokens that can contain embedded newlines need to adjust our current
428 // line number.
429 if (Tok.getKind() == tok::comment)
430 Callbacks->HandleNewlinesInToken(&S[0], S.size());
431 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 Callbacks->SetEmittedTokensOnThisLine();
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner6f688e12007-10-10 20:45:16 +0000434 if (Tok.is(tok::eof)) break;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattner6f688e12007-10-10 20:45:16 +0000436 PrevTok = Tok;
437 PP.Lex(Tok);
438 }
Chris Lattner59076ab2009-02-06 05:56:11 +0000439}
440
Benjamin Kramer3ff2a4b2010-01-19 17:42:20 +0000441typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
442static int MacroIDCompare(const void* a, const void* b) {
443 const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a);
444 const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b);
445 return LHS->first->getName().compare(RHS->first->getName());
Chris Lattner2a2bb182009-02-10 22:28:19 +0000446}
Chris Lattner59076ab2009-02-06 05:56:11 +0000447
Daniel Dunbar775bee72009-11-11 10:07:22 +0000448static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
Eli Friedmanf1db5852009-05-19 01:32:34 +0000449 // -dM mode just scans and ignores all tokens in the files, then dumps out
450 // the macro table at the end.
451 PP.EnterMainSourceFile();
452
453 Token Tok;
454 do PP.Lex(Tok);
455 while (Tok.isNot(tok::eof));
456
Benjamin Kramer3ff2a4b2010-01-19 17:42:20 +0000457 llvm::SmallVector<id_macro_pair, 128>
458 MacrosByID(PP.macro_begin(), PP.macro_end());
459 llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000460
461 for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
462 MacroInfo &MI = *MacrosByID[i].second;
Mike Stump1eb44332009-09-09 15:08:12 +0000463 // Ignore computed macros like __LINE__ and friends.
Eli Friedmanf1db5852009-05-19 01:32:34 +0000464 if (MI.isBuiltinMacro()) continue;
465
466 PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
Benjamin Kramer3ff2a4b2010-01-19 17:42:20 +0000467 *OS << '\n';
Eli Friedmanf1db5852009-05-19 01:32:34 +0000468 }
469}
470
Chris Lattner59076ab2009-02-06 05:56:11 +0000471/// DoPrintPreprocessedInput - This implements -E mode.
472///
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000473void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
Daniel Dunbar29cf7462009-11-11 10:07:44 +0000474 const PreprocessorOutputOptions &Opts) {
Daniel Dunbar775bee72009-11-11 10:07:22 +0000475 // Show macros with no output is handled specially.
476 if (!Opts.ShowCPP) {
477 assert(Opts.ShowMacros && "Not yet implemented!");
478 DoPrintMacros(PP, OS);
479 return;
480 }
481
Chris Lattner59076ab2009-02-06 05:56:11 +0000482 // Inform the preprocessor whether we want it to retain comments or not, due
483 // to -C or -CC.
Daniel Dunbar775bee72009-11-11 10:07:22 +0000484 PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
Eli Friedmanf54fce82009-05-19 01:02:07 +0000485
486 OS->SetBufferSize(64*1024);
Chris Lattnerd82df3a2009-04-12 01:56:53 +0000487
Eli Friedman12d3b1d2009-05-19 03:06:47 +0000488 PrintPPOutputPPCallbacks *Callbacks =
Daniel Dunbar775bee72009-11-11 10:07:22 +0000489 new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
490 Opts.ShowMacros);
Eli Friedmanf1db5852009-05-19 01:32:34 +0000491 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
492 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
493 Callbacks));
Chris Lattner59076ab2009-02-06 05:56:11 +0000494
Eli Friedmanf1db5852009-05-19 01:32:34 +0000495 PP.setPPCallbacks(Callbacks);
Chris Lattner59076ab2009-02-06 05:56:11 +0000496
Eli Friedmanf1db5852009-05-19 01:32:34 +0000497 // After we have configured the preprocessor, enter the main file.
498 PP.EnterMainSourceFile();
Chris Lattner59076ab2009-02-06 05:56:11 +0000499
Eli Friedmanf1db5852009-05-19 01:32:34 +0000500 // Consume all of the tokens that come from the predefines buffer. Those
501 // should not be emitted into the output and are guaranteed to be at the
502 // start.
503 const SourceManager &SourceMgr = PP.getSourceManager();
504 Token Tok;
505 do PP.Lex(Tok);
506 while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
507 !strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
508 "<built-in>"));
Chris Lattner59076ab2009-02-06 05:56:11 +0000509
Eli Friedmanf1db5852009-05-19 01:32:34 +0000510 // Read all the preprocessed tokens, printing them out to the stream.
511 PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
512 *OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000513}
514