blob: ffa971ea405c388521caf2c5fe2e806d3dc0dc69 [file] [log] [blame]
Chris Lattner09e3cdf2006-07-04 19:04:05 +00001//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
15#include "clang.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Lex/Pragma.h"
18#include "clang/Basic/SourceManager.h"
19#include "llvm/Support/CommandLine.h"
Chris Lattnerdeb37012006-07-04 19:24:06 +000020// NOTE: we use stdio because it is empirically much faster than iostreams.
21#include <cstdio>
Chris Lattner09e3cdf2006-07-04 19:04:05 +000022using namespace llvm;
23using namespace clang;
24
25static cl::opt<bool>
26DisableLineMarkers("P", cl::desc("Disable linemarker output in -E mode"));
27
28static unsigned EModeCurLine;
29static std::string EModeCurFilename;
30static Preprocessor *EModePP;
31static bool EmodeEmittedTokensOnThisLine;
32static DirectoryLookup::DirType EmodeFileType =DirectoryLookup::NormalHeaderDir;
33
34static void MoveToLine(unsigned LineNo) {
35 // If this line is "close enough" to the original line, just print newlines,
36 // otherwise print a #line directive.
37 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000038 unsigned CurLine = EModeCurLine;
39 //static const char Newlines[] = "\n\n\n\n\n\n\n\n";
40 for (; CurLine != LineNo; ++CurLine)
41 putchar_unlocked('\n');
42 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +000043 } else {
44 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000045 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000046 EmodeEmittedTokensOnThisLine = false;
47 }
48
49 EModeCurLine = LineNo;
50 if (DisableLineMarkers) return;
51
Chris Lattnerdeb37012006-07-04 19:24:06 +000052 printf("# %d %s", LineNo, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000053
54 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000055 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000056 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000057 printf(" 3 4");
58 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000059 }
60}
61
62/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
63/// it invokes this handler. Update our conception of the current
64static void HandleFileChange(SourceLocation Loc,
65 Preprocessor::FileChangeReason Reason,
66 DirectoryLookup::DirType FileType) {
67 SourceManager &SourceMgr = EModePP->getSourceManager();
68
69 // Unless we are exiting a #include, make sure to skip ahead to the line the
70 // #include directive was at.
71 if (Reason == Preprocessor::EnterFile) {
72 SourceLocation IncludeLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
73 MoveToLine(SourceMgr.getLineNumber(IncludeLoc));
74 } else if (Reason == Preprocessor::SystemHeaderPragma) {
75 MoveToLine(SourceMgr.getLineNumber(Loc));
76
77 // TODO GCC emits the # directive for this directive on the line AFTER the
78 // directive and emits a bunch of spaces that aren't needed. Emulate this
79 // strange behavior.
80 }
81
82 EModeCurLine = SourceMgr.getLineNumber(Loc);
83 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
84 EmodeFileType = FileType;
85
86 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000087 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000088 EmodeEmittedTokensOnThisLine = false;
89 }
90
91 if (DisableLineMarkers) return;
92
Chris Lattnerdeb37012006-07-04 19:24:06 +000093 printf("# %d %s", EModeCurLine, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000094 switch (Reason) {
95 case Preprocessor::EnterFile:
Chris Lattnerdeb37012006-07-04 19:24:06 +000096 printf(" 1");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000097 break;
98 case Preprocessor::ExitFile:
Chris Lattnerdeb37012006-07-04 19:24:06 +000099 printf(" 2");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000100 break;
101 case Preprocessor::SystemHeaderPragma: break;
102 case Preprocessor::RenameFile: break;
103 }
104
105 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000106 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000107 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000108 printf(" 3 4");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000109
Chris Lattnerdeb37012006-07-04 19:24:06 +0000110 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000111}
112
113static void HandleIdent(SourceLocation Loc, const std::string &Val) {
114 SourceManager &SourceMgr = EModePP->getSourceManager();
115 MoveToLine(SourceMgr.getLineNumber(Loc));
116
Chris Lattnerdeb37012006-07-04 19:24:06 +0000117 printf("#ident %s", Val.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000118 EmodeEmittedTokensOnThisLine = true;
119}
120
121/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
122/// is called for the first token on each new line.
123static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
124 // Figure out what line we went to and insert the appropriate number of
125 // newline characters.
126 unsigned LineNo = PP.getSourceManager().getLineNumber(Tok.getLocation());
127
128 // Move to the specified line.
129 MoveToLine(LineNo);
130
131
132 // Print out space characters so that the first token on a line is
133 // indented for easy reading.
134 unsigned ColNo =
135 PP.getSourceManager().getColumnNumber(Tok.getLocation());
136
137 // This hack prevents stuff like:
138 // #define HASH #
139 // HASH define foo bar
140 // From having the # character end up at column 1, which makes it so it
141 // is not handled as a #define next time through the preprocessor if in
142 // -fpreprocessed mode.
143 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000144 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000145
146 // Otherwise, indent the appropriate number of spaces.
147 for (; ColNo > 1; --ColNo)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000148 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000149}
150
Chris Lattner5de858c2006-07-04 19:04:44 +0000151namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000152struct UnknownPragmaHandler : public PragmaHandler {
153 const char *Prefix;
154 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
155 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
156 // Figure out what line we went to and insert the appropriate number of
157 // newline characters.
158 MoveToLine(PP.getSourceManager().getLineNumber(PragmaTok.getLocation()));
Chris Lattnerdeb37012006-07-04 19:24:06 +0000159 printf(Prefix);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000160
161 // Read and print all of the pragma tokens.
162 while (PragmaTok.getKind() != tok::eom) {
163 if (PragmaTok.hasLeadingSpace())
Chris Lattnerdeb37012006-07-04 19:24:06 +0000164 putchar_unlocked(' ');
165 printf("%s", PP.getSpelling(PragmaTok).c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000166 PP.LexUnexpandedToken(PragmaTok);
167 }
Chris Lattnerdeb37012006-07-04 19:24:06 +0000168 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000169 }
170};
Chris Lattner5de858c2006-07-04 19:04:44 +0000171} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000172
173/// DoPrintPreprocessedInput - This implements -E mode.
174void clang::DoPrintPreprocessedInput(Preprocessor &PP) {
175 LexerToken Tok;
176 char Buffer[256];
177 EModeCurLine = 0;
178 EModeCurFilename = "\"<uninit>\"";
179 PP.setFileChangeHandler(HandleFileChange);
180 PP.setIdentHandler(HandleIdent);
181 EModePP = &PP;
182 EmodeEmittedTokensOnThisLine = false;
183
184 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
185 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
186 do {
187 PP.Lex(Tok);
188
189 // If this token is at the start of a line. Emit the \n and indentation.
190 // FIXME: this shouldn't use the isAtStartOfLine flag. This should use a
191 // "newline callback" from the lexer.
192 // FIXME: For some tests, this fails just because there is no col# info from
193 // macro expansions!
194 if (Tok.isAtStartOfLine()) {
195 HandleFirstTokOnLine(Tok, PP);
196 } else if (Tok.hasLeadingSpace()) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000197 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000198 }
199
200 if (Tok.getLength() < 256) {
201 unsigned Len = PP.getSpelling(Tok, Buffer);
202 Buffer[Len] = 0;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000203 fwrite(Buffer, Len, 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000204 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000205 std::string S = PP.getSpelling(Tok);
206 fwrite(&S[0], S.size(), 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000207 }
208 EmodeEmittedTokensOnThisLine = true;
209 } while (Tok.getKind() != tok::eof);
Chris Lattnerdeb37012006-07-04 19:24:06 +0000210 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000211}
212