blob: 0d68ee24d0db0e6aff6f6cc8735c4edeae59d4ce [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
Chris Lattner728b4dc2006-07-04 21:28:37 +000034
35
36/// MoveToLine - Move the output to the source line specified by the location
37/// object. We can do this by emitting some number of \n's, or be emitting a
38/// #line directive.
Chris Lattner3338ba82006-07-04 21:19:39 +000039static void MoveToLine(SourceLocation Loc) {
40 if (DisableLineMarkers) return;
41
42 unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc);
43
Chris Lattner09e3cdf2006-07-04 19:04:05 +000044 // If this line is "close enough" to the original line, just print newlines,
45 // otherwise print a #line directive.
46 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000047 unsigned CurLine = EModeCurLine;
Chris Lattnerdeb37012006-07-04 19:24:06 +000048 for (; CurLine != LineNo; ++CurLine)
49 putchar_unlocked('\n');
50 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +000051 } else {
52 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000053 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000054 EmodeEmittedTokensOnThisLine = false;
55 }
56
57 EModeCurLine = LineNo;
58 if (DisableLineMarkers) return;
59
Chris Lattnerdeb37012006-07-04 19:24:06 +000060 printf("# %d %s", LineNo, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000061
62 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000063 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000064 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000065 printf(" 3 4");
66 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000067 }
68}
69
70/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
71/// it invokes this handler. Update our conception of the current
72static void HandleFileChange(SourceLocation Loc,
73 Preprocessor::FileChangeReason Reason,
74 DirectoryLookup::DirType FileType) {
Chris Lattner03cbe1f2006-07-04 21:24:33 +000075 if (DisableLineMarkers) return;
Chris Lattner73b6a2f2006-07-04 19:40:52 +000076
Chris Lattner09e3cdf2006-07-04 19:04:05 +000077 // Unless we are exiting a #include, make sure to skip ahead to the line the
78 // #include directive was at.
Chris Lattnerff3f5f42006-07-04 21:25:59 +000079 SourceManager &SourceMgr = EModePP->getSourceManager();
Chris Lattner09e3cdf2006-07-04 19:04:05 +000080 if (Reason == Preprocessor::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +000081 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattner09e3cdf2006-07-04 19:04:05 +000082 } else if (Reason == Preprocessor::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +000083 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +000084
85 // TODO GCC emits the # directive for this directive on the line AFTER the
86 // directive and emits a bunch of spaces that aren't needed. Emulate this
87 // strange behavior.
88 }
89
90 EModeCurLine = SourceMgr.getLineNumber(Loc);
91 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
92 EmodeFileType = FileType;
93
94 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000095 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000096 EmodeEmittedTokensOnThisLine = false;
97 }
98
99 if (DisableLineMarkers) return;
100
Chris Lattnerdeb37012006-07-04 19:24:06 +0000101 printf("# %d %s", EModeCurLine, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000102 switch (Reason) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000103 case Preprocessor::EnterFile:
104 printf(" 1");
105 break;
106 case Preprocessor::ExitFile:
107 printf(" 2");
108 break;
109 case Preprocessor::SystemHeaderPragma: break;
110 case Preprocessor::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000111 }
112
113 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000114 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000115 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000116 printf(" 3 4");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000117
Chris Lattnerdeb37012006-07-04 19:24:06 +0000118 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000119}
120
Chris Lattner728b4dc2006-07-04 21:28:37 +0000121/// HandleIdent - Handle #ident directives when read by the preprocessor.
122///
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000123static void HandleIdent(SourceLocation Loc, const std::string &Val) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000124 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000125
Chris Lattnerdeb37012006-07-04 19:24:06 +0000126 printf("#ident %s", Val.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000127 EmodeEmittedTokensOnThisLine = true;
128}
129
130/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
131/// is called for the first token on each new line.
132static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
133 // Figure out what line we went to and insert the appropriate number of
134 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000135 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000136
137 // Print out space characters so that the first token on a line is
138 // indented for easy reading.
139 unsigned ColNo =
140 PP.getSourceManager().getColumnNumber(Tok.getLocation());
141
142 // This hack prevents stuff like:
143 // #define HASH #
144 // HASH define foo bar
145 // From having the # character end up at column 1, which makes it so it
146 // is not handled as a #define next time through the preprocessor if in
147 // -fpreprocessed mode.
148 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000149 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000150
151 // Otherwise, indent the appropriate number of spaces.
152 for (; ColNo > 1; --ColNo)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000153 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000154}
155
Chris Lattner5de858c2006-07-04 19:04:44 +0000156namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000157struct UnknownPragmaHandler : public PragmaHandler {
158 const char *Prefix;
159 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
160 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
161 // Figure out what line we went to and insert the appropriate number of
162 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000163 MoveToLine(PragmaTok.getLocation());
Chris Lattnerdeb37012006-07-04 19:24:06 +0000164 printf(Prefix);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000165
166 // Read and print all of the pragma tokens.
167 while (PragmaTok.getKind() != tok::eom) {
168 if (PragmaTok.hasLeadingSpace())
Chris Lattnerdeb37012006-07-04 19:24:06 +0000169 putchar_unlocked(' ');
170 printf("%s", PP.getSpelling(PragmaTok).c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000171 PP.LexUnexpandedToken(PragmaTok);
172 }
Chris Lattnerdeb37012006-07-04 19:24:06 +0000173 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000174 }
175};
Chris Lattner5de858c2006-07-04 19:04:44 +0000176} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000177
178/// DoPrintPreprocessedInput - This implements -E mode.
Chris Lattner728b4dc2006-07-04 21:28:37 +0000179///
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000180void clang::DoPrintPreprocessedInput(Preprocessor &PP) {
181 LexerToken Tok;
182 char Buffer[256];
183 EModeCurLine = 0;
184 EModeCurFilename = "\"<uninit>\"";
185 PP.setFileChangeHandler(HandleFileChange);
186 PP.setIdentHandler(HandleIdent);
187 EModePP = &PP;
188 EmodeEmittedTokensOnThisLine = false;
189
190 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
191 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
192 do {
193 PP.Lex(Tok);
194
195 // If this token is at the start of a line. Emit the \n and indentation.
196 // FIXME: this shouldn't use the isAtStartOfLine flag. This should use a
197 // "newline callback" from the lexer.
198 // FIXME: For some tests, this fails just because there is no col# info from
199 // macro expansions!
200 if (Tok.isAtStartOfLine()) {
201 HandleFirstTokOnLine(Tok, PP);
202 } else if (Tok.hasLeadingSpace()) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000203 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000204 }
205
206 if (Tok.getLength() < 256) {
207 unsigned Len = PP.getSpelling(Tok, Buffer);
208 Buffer[Len] = 0;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000209 fwrite(Buffer, Len, 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000210 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000211 std::string S = PP.getSpelling(Tok);
212 fwrite(&S[0], S.size(), 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000213 }
214 EmodeEmittedTokensOnThisLine = true;
215 } while (Tok.getKind() != tok::eof);
Chris Lattnerdeb37012006-07-04 19:24:06 +0000216 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000217}
218