blob: d5b91b2e16564ca468b07e7b4ddb694eb2bd1b25 [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 Lattner3338ba82006-07-04 21:19:39 +000034static void MoveToLine(SourceLocation Loc) {
35 if (DisableLineMarkers) return;
36
37 unsigned LineNo = EModePP->getSourceManager().getLineNumber(Loc);
38
Chris Lattner09e3cdf2006-07-04 19:04:05 +000039 // If this line is "close enough" to the original line, just print newlines,
40 // otherwise print a #line directive.
41 if (LineNo-EModeCurLine < 8) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000042 unsigned CurLine = EModeCurLine;
Chris Lattnerdeb37012006-07-04 19:24:06 +000043 for (; CurLine != LineNo; ++CurLine)
44 putchar_unlocked('\n');
45 EModeCurLine = CurLine;
Chris Lattner09e3cdf2006-07-04 19:04:05 +000046 } else {
47 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000048 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000049 EmodeEmittedTokensOnThisLine = false;
50 }
51
52 EModeCurLine = LineNo;
53 if (DisableLineMarkers) return;
54
Chris Lattnerdeb37012006-07-04 19:24:06 +000055 printf("# %d %s", LineNo, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +000056
57 if (EmodeFileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000058 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +000059 else if (EmodeFileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +000060 printf(" 3 4");
61 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000062 }
63}
64
65/// HandleFileChange - Whenever the preprocessor enters or exits a #include file
66/// it invokes this handler. Update our conception of the current
67static void HandleFileChange(SourceLocation Loc,
68 Preprocessor::FileChangeReason Reason,
69 DirectoryLookup::DirType FileType) {
70 SourceManager &SourceMgr = EModePP->getSourceManager();
71
Chris Lattner73b6a2f2006-07-04 19:40:52 +000072 if (DisableLineMarkers) {
Chris Lattner73b6a2f2006-07-04 19:40:52 +000073 EmodeFileType = FileType;
74 return;
75 }
76
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.
79 if (Reason == Preprocessor::EnterFile) {
Chris Lattner3338ba82006-07-04 21:19:39 +000080 MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
Chris Lattner09e3cdf2006-07-04 19:04:05 +000081 } else if (Reason == Preprocessor::SystemHeaderPragma) {
Chris Lattner3338ba82006-07-04 21:19:39 +000082 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +000083
84 // TODO GCC emits the # directive for this directive on the line AFTER the
85 // directive and emits a bunch of spaces that aren't needed. Emulate this
86 // strange behavior.
87 }
88
89 EModeCurLine = SourceMgr.getLineNumber(Loc);
90 EModeCurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc));
91 EmodeFileType = FileType;
92
93 if (EmodeEmittedTokensOnThisLine) {
Chris Lattnerdeb37012006-07-04 19:24:06 +000094 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +000095 EmodeEmittedTokensOnThisLine = false;
96 }
97
98 if (DisableLineMarkers) return;
99
Chris Lattnerdeb37012006-07-04 19:24:06 +0000100 printf("# %d %s", EModeCurLine, EModeCurFilename.c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000101 switch (Reason) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000102 case Preprocessor::EnterFile:
103 printf(" 1");
104 break;
105 case Preprocessor::ExitFile:
106 printf(" 2");
107 break;
108 case Preprocessor::SystemHeaderPragma: break;
109 case Preprocessor::RenameFile: break;
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000110 }
111
112 if (FileType == DirectoryLookup::SystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000113 printf(" 3");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000114 else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000115 printf(" 3 4");
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000116
Chris Lattnerdeb37012006-07-04 19:24:06 +0000117 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000118}
119
120static void HandleIdent(SourceLocation Loc, const std::string &Val) {
Chris Lattner3338ba82006-07-04 21:19:39 +0000121 MoveToLine(Loc);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000122
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.
Chris Lattner3338ba82006-07-04 21:19:39 +0000132 MoveToLine(Tok.getLocation());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000133
134 // Print out space characters so that the first token on a line is
135 // indented for easy reading.
136 unsigned ColNo =
137 PP.getSourceManager().getColumnNumber(Tok.getLocation());
138
139 // This hack prevents stuff like:
140 // #define HASH #
141 // HASH define foo bar
142 // From having the # character end up at column 1, which makes it so it
143 // is not handled as a #define next time through the preprocessor if in
144 // -fpreprocessed mode.
145 if (ColNo <= 1 && Tok.getKind() == tok::hash)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000146 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000147
148 // Otherwise, indent the appropriate number of spaces.
149 for (; ColNo > 1; --ColNo)
Chris Lattnerdeb37012006-07-04 19:24:06 +0000150 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000151}
152
Chris Lattner5de858c2006-07-04 19:04:44 +0000153namespace {
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000154struct UnknownPragmaHandler : public PragmaHandler {
155 const char *Prefix;
156 UnknownPragmaHandler(const char *prefix) : PragmaHandler(0), Prefix(prefix) {}
157 virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
158 // Figure out what line we went to and insert the appropriate number of
159 // newline characters.
Chris Lattner3338ba82006-07-04 21:19:39 +0000160 MoveToLine(PragmaTok.getLocation());
Chris Lattnerdeb37012006-07-04 19:24:06 +0000161 printf(Prefix);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000162
163 // Read and print all of the pragma tokens.
164 while (PragmaTok.getKind() != tok::eom) {
165 if (PragmaTok.hasLeadingSpace())
Chris Lattnerdeb37012006-07-04 19:24:06 +0000166 putchar_unlocked(' ');
167 printf("%s", PP.getSpelling(PragmaTok).c_str());
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000168 PP.LexUnexpandedToken(PragmaTok);
169 }
Chris Lattnerdeb37012006-07-04 19:24:06 +0000170 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000171 }
172};
Chris Lattner5de858c2006-07-04 19:04:44 +0000173} // end anonymous namespace
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000174
175/// DoPrintPreprocessedInput - This implements -E mode.
176void clang::DoPrintPreprocessedInput(Preprocessor &PP) {
177 LexerToken Tok;
178 char Buffer[256];
179 EModeCurLine = 0;
180 EModeCurFilename = "\"<uninit>\"";
181 PP.setFileChangeHandler(HandleFileChange);
182 PP.setIdentHandler(HandleIdent);
183 EModePP = &PP;
184 EmodeEmittedTokensOnThisLine = false;
185
186 PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma"));
187 PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC"));
188 do {
189 PP.Lex(Tok);
190
191 // If this token is at the start of a line. Emit the \n and indentation.
192 // FIXME: this shouldn't use the isAtStartOfLine flag. This should use a
193 // "newline callback" from the lexer.
194 // FIXME: For some tests, this fails just because there is no col# info from
195 // macro expansions!
196 if (Tok.isAtStartOfLine()) {
197 HandleFirstTokOnLine(Tok, PP);
198 } else if (Tok.hasLeadingSpace()) {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000199 putchar_unlocked(' ');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000200 }
201
202 if (Tok.getLength() < 256) {
203 unsigned Len = PP.getSpelling(Tok, Buffer);
204 Buffer[Len] = 0;
Chris Lattnerdeb37012006-07-04 19:24:06 +0000205 fwrite(Buffer, Len, 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000206 } else {
Chris Lattnerdeb37012006-07-04 19:24:06 +0000207 std::string S = PP.getSpelling(Tok);
208 fwrite(&S[0], S.size(), 1, stdout);
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000209 }
210 EmodeEmittedTokensOnThisLine = true;
211 } while (Tok.getKind() != tok::eof);
Chris Lattnerdeb37012006-07-04 19:24:06 +0000212 putchar_unlocked('\n');
Chris Lattner09e3cdf2006-07-04 19:04:05 +0000213}
214