blob: 27e8195511932821be0d80db12f49f90eff6fac9 [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;
Chris Lattnerdeb37012006-07-04 19:24:06 +000039 for (; CurLine != LineNo; ++CurLine)
40 putchar_unlocked('\n');
41 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +000042 } else {
43 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000044 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000045 EmodeEmittedTokensOnThisLine = false;
46 }
47
48 EModeCurLine = LineNo;
49 if (DisableLineMarkers) return;
50
Chris Lattnerdeb37012006-07-04 19:24:06 +000051 printf("# %d %s", LineNo, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000052
53 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000054 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000055 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000056 printf(" 3 4");
57 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000058 }
59}
60
61/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
62/// it invokes this handler. Update our conception of the current
63static void HandleFileChange(SourceLocation Loc,
64 Preprocessor::FileChangeReason Reason,
65 DirectoryLookup::DirType FileType) {
66 SourceManager &SourceMgr = EModePP->getSourceManager();
67
Chris Lattner73b6a2f2006-07-04 19:40:52 +000068 if (DisableLineMarkers) {
69 EModeCurLine = SourceMgr.getLineNumber(Loc);
70 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
71 EmodeFileType = FileType;
72 return;
73 }
74
Chris Lattner09e3cdf2006-07-04 19:04:05 +000075 // Unless we are exiting a #include, make sure to skip ahead to the line the
76 // #include directive was at.
77 if (Reason == Preprocessor::EnterFile) {
78 SourceLocation IncludeLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
79 MoveToLine(SourceMgr.getLineNumber(IncludeLoc));
80 } else if (Reason == Preprocessor::SystemHeaderPragma) {
81 MoveToLine(SourceMgr.getLineNumber(Loc));
82
83 // TODO GCC emits the # directive for this directive on the line AFTER the
84 // directive and emits a bunch of spaces that aren't needed. Emulate this
85 // strange behavior.
86 }
87
88 EModeCurLine = SourceMgr.getLineNumber(Loc);
89 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
90 EmodeFileType = FileType;
91
92 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000093 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000094 EmodeEmittedTokensOnThisLine = false;
95 }
96
97 if (DisableLineMarkers) return;
98
Chris Lattnerdeb37012006-07-04 19:24:06 +000099 printf("# %d %s", EModeCurLine, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000100 switch (Reason) {
101 case Preprocessor::EnterFile:
Chris Lattnerdeb37012006-07-04 19:24:06 +0000102 printf(" 1");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000103 break;
104 case Preprocessor::ExitFile:
Chris Lattnerdeb37012006-07-04 19:24:06 +0000105 printf(" 2");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000106 break;
107 case Preprocessor::SystemHeaderPragma: break;
108 case Preprocessor::RenameFile: break;
109 }
110
111 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000112 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000113 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000114 printf(" 3 4");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000115
Chris Lattnerdeb37012006-07-04 19:24:06 +0000116 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000117}
118
119static void HandleIdent(SourceLocation Loc, const std::string &Val) {
120 SourceManager &SourceMgr = EModePP->getSourceManager();
121 MoveToLine(SourceMgr.getLineNumber(Loc));
122
Chris Lattnerdeb37012006-07-04 19:24:06 +0000123 printf("#ident %s", Val.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000124 EmodeEmittedTokensOnThisLine = true;
125}
126
127/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
128/// is called for the first token on each new line.
129static void HandleFirstTokOnLine(LexerToken &Tok, Preprocessor &PP) {
130 // Figure out what line we went to and insert the appropriate number of
131 // newline characters.
132 unsigned LineNo = PP.getSourceManager().getLineNumber(Tok.getLocation());
133
134 // Move to the specified line.
135 MoveToLine(LineNo);
136
137
138 // Print out space characters so that the first token on a line is
139 // indented for easy reading.
140 unsigned ColNo =
141 PP.getSourceManager().getColumnNumber(Tok.getLocation());
142
143 // This hack prevents stuff like:
144 // #define HASH #
145 // HASH define foo bar
146 // From having the # character end up at column 1, which makes it so it
147 // is not handled as a #define next time through the preprocessor if in
148 // -fpreprocessed mode.
149 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000150 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000151
152 // Otherwise, indent the appropriate number of spaces.
153 for (; ColNo > 1; --ColNo)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000154 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000155}
156
Chris Lattner5de858c2006-07-04 19:04:44 +0000157namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000158struct UnknownPragmaHandler : public PragmaHandler {
159 const char *Prefix;
160 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
161 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
162 // Figure out what line we went to and insert the appropriate number of
163 // newline characters.
164 MoveToLine(PP.getSourceManager().getLineNumber(PragmaTok.getLocation()));
Chris Lattnerdeb37012006-07-04 19:24:06 +0000165 printf(Prefix);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000166
167 // Read and print all of the pragma tokens.
168 while (PragmaTok.getKind() != tok::eom) {
169 if (PragmaTok.hasLeadingSpace())
Chris Lattnerdeb37012006-07-04 19:24:06 +0000170 putchar_unlocked(' ');
171 printf("%s", PP.getSpelling(PragmaTok).c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000172 PP.LexUnexpandedToken(PragmaTok);
173 }
Chris Lattnerdeb37012006-07-04 19:24:06 +0000174 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000175 }
176};
Chris Lattner5de858c2006-07-04 19:04:44 +0000177} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000178
179/// DoPrintPreprocessedInput - This implements -E mode.
180void 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